Math, 29
<FILEB>
<CHANGES>
final int n = getDimension();
for (int i = 0; i < n; i++) {
res.setEntry(i, this.getEntry(i) / v.getEntry(i));
<CHANGEE>
<CHANGES>
if (v.isNaN() || v.isInfinite()) {
final int n = getDimension();
for (int i = 0; i < n; i++) {
final double y = v.getEntry(i);
if (Double.isNaN(y)) {
res.setEntry(i, Double.NaN);
} else if (Double.isInfinite(y)) {
final double x = this.getEntry(i);
res.setEntry(i, x * y);
}
}
}
<CHANGEE>
<FILEE>
<FILEB>
@Override
public double dotProduct(RealVector v) {
if(v instanceof OpenMapRealVector) {
return dotProduct((OpenMapRealVector)v);
} else {
return super.dotProduct(v);
}
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeDivide(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
/*
* MATH-803: it is not sufficient to loop through non zero entries of
* this only. Indeed, if this[i] = 0d and v[i] = 0d, then
* this[i] / v[i] = NaN, and not 0d.
*/
<CHANGES>
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() / v.getEntry(iter.key()));
<CHANGEE>
}
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeMultiply(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() * v.getEntry(iter.key()));
}
/*
* MATH-803: the above loop assumes that 0d * x = 0d for any double x,
* which allows to consider only the non-zero entries of this. However,
* this fails if this[i] == 0d and (v[i] = NaN or v[i] = Infinity).
*
* These special cases are handled below.
*/
<CHANGES>
<CHANGEE>
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector getSubVector(int index, int n) {
checkIndex(index);
if (n < 0) {
throw new NotPositiveException(LocalizedFormats.NUMBER_OF_ELEMENTS_SHOULD_BE_POSITIVE, n);
}
checkIndex(index + n - 1);
OpenMapRealVector<SCANS>, 0.0);
this.epsilon = epsilon;
}
/**
* Create from an array.
* Only non-zero entries will be stored.
*
* @param values Set of values to create from.
*/
public OpenMapRealVector(double[] values) {
this(values, DEFAULT_ZERO_TOLERANCE);
}
/**
* Create from an array, specifying zero tolerance.
* Only non-zero entries will be stored.
*
* @param values Set of values to create from.
* @param epsilon Tolerance below which a value is considered zero.
*/
public OpenMapRealVector(double[] values, double epsilon) {
virtualSize = values.length;
entries = new OpenIntToDoubleHashMap(0.0);
this.epsilon = epsilon;
for (int key = 0; key < values.length; key++) {
double value = values[key];
if (!isDefaultValue(value)) {
entries.put(key, value);
}
}
}
/**
* Create from an array.
* Only non-zero entries will be stored.
*
* @param values The set of values to create from
*/
public OpenMapRealVector(Double[] values) {
this(values, DEFAULT_ZERO_TOLERANCE);
}
/**
* Create from an array.
* Only non-zero entries will be stored.
*
* @param values Set of values to create from.
* @param epsilon Tolerance below which a value is considered zero.
*/
public OpenMapRealVector(Double[] values, double epsilon) {
virtualSize = values.length;
entries = new OpenIntToDoubleHashMap(0.0);
this.epsilon = epsilon;
for (int key = 0; key < values.length; key++) {
double value = values[key].doubleValue();
if (!isDefaultValue(value)) {
entries.put(key, value);
}
}
}
/**
* Copy constructor.
*
* @param v Instance to copy from.
*/
public OpenMapRealVector(OpenMapRealVector v) {
virtualSize = v.getDimension();
entries = new OpenIntToDoubleHashMap(v.getEntries());
epsilon = v.epsilon;
}
/**
* Generic copy constructor
Math, 29
<FILEB>
<CHANGES>
final int n = getDimension();
for (int i = 0; i < n; i++) {
res.setEntry(i, this.getEntry(i) / v.getEntry(i));
<CHANGEE>
<CHANGES>
if (v.isNaN() || v.isInfinite()) {
final int n = getDimension();
for (int i = 0; i < n; i++) {
final double y = v.getEntry(i);
if (Double.isNaN(y)) {
res.setEntry(i, Double.NaN);
} else if (Double.isInfinite(y)) {
final double x = this.getEntry(i);
res.setEntry(i, x * y);
}
}
}
<CHANGEE>
<FILEE>
<FILEB>
@Override
public double dotProduct(RealVector v) {
if(v instanceof OpenMapRealVector) {
return dotProduct((OpenMapRealVector)v);
} else {
return super.dotProduct(v);
}
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeDivide(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
/*
* MATH-803: it is not sufficient to loop through non zero entries of
* this only. Indeed, if this[i] = 0d and v[i] = 0d, then
* this[i] / v[i] = NaN, and not 0d.
*/
<CHANGES>
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() / v.getEntry(iter.key()));
<CHANGEE>
}
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeMultiply(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() * v.getEntry(iter.key()));
}
/*
* MATH-803: the above loop assumes that 0d * x = 0d for any double x,
* which allows to consider only the non-zero entries of this. However,
* this fails if this[i] == 0d and (v[i] = NaN or v[i] = Infinity).
*
* These special cases are handled below.
*/
<CHANGES>
<CHANGEE>
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector getSubVector(int index, int n) {
checkIndex(index);
if (n < 0) {
throw new NotPositiveException(LocalizedFormats.NUMBER_OF_ELEMENTS_SHOULD_BE_POSITIVE, n);
}
checkIndex(index + n - 1);
OpenMapRealVector<SCANS>Access.containsKey(key)) {
res.setEntry(key, randomAccess.get(key) + iter.value());
} else {
res.setEntry(key, iter.value());
}
}
return res;
}
/**
* Optimized method to append a OpenMapRealVector.
* @param v vector to append
* @return The result of appending {@code v} to self
*/
public OpenMapRealVector append(OpenMapRealVector v) {
OpenMapRealVector res = new OpenMapRealVector(this, v.getDimension());
Iterator iter = v.entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key() + virtualSize, iter.value());
}
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector append(RealVector v) {
if (v instanceof OpenMapRealVector) {
return append((OpenMapRealVector) v);
} else {
final OpenMapRealVector res = new OpenMapRealVector(this, v.getDimension());
for (int i = 0; i < v.getDimension(); i++) {
res.setEntry(i + virtualSize, v.getEntry(i));
}
return res;
}
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector append(double d) {
OpenMapRealVector res = new OpenMapRealVector(this, 1);
res.setEntry(virtualSize, d);
return res;
}
/**
* {@inheritDoc}
* @since 2.1
*/
@Override
public OpenMapRealVector copy() {
return new OpenMapRealVector(this);
}
/**
* Optimized method to compute the dot product with an OpenMapRealVector.
* It iterates over the smallest of the two.
*
* @param v Cector to compute the dot product with.
* @return the dot product of {@code this} and {@code v}.
* @throws org.apache.commons.math3.exception.DimensionMismatchException
* if the dimensions do not match.
*/
public double dotProduct(OpenMapRealVector v) {
checkVectorDimensions(v.getDimension());
boolean thisIs
Math, 29
<FILEB>
<CHANGES>
final int n = getDimension();
for (int i = 0; i < n; i++) {
res.setEntry(i, this.getEntry(i) / v.getEntry(i));
<CHANGEE>
<CHANGES>
if (v.isNaN() || v.isInfinite()) {
final int n = getDimension();
for (int i = 0; i < n; i++) {
final double y = v.getEntry(i);
if (Double.isNaN(y)) {
res.setEntry(i, Double.NaN);
} else if (Double.isInfinite(y)) {
final double x = this.getEntry(i);
res.setEntry(i, x * y);
}
}
}
<CHANGEE>
<FILEE>
<FILEB>
@Override
public double dotProduct(RealVector v) {
if(v instanceof OpenMapRealVector) {
return dotProduct((OpenMapRealVector)v);
} else {
return super.dotProduct(v);
}
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeDivide(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
/*
* MATH-803: it is not sufficient to loop through non zero entries of
* this only. Indeed, if this[i] = 0d and v[i] = 0d, then
* this[i] / v[i] = NaN, and not 0d.
*/
<CHANGES>
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() / v.getEntry(iter.key()));
<CHANGEE>
}
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeMultiply(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() * v.getEntry(iter.key()));
}
/*
* MATH-803: the above loop assumes that 0d * x = 0d for any double x,
* which allows to consider only the non-zero entries of this. However,
* this fails if this[i] == 0d and (v[i] = NaN or v[i] = Infinity).
*
* These special cases are handled below.
*/
<CHANGES>
<CHANGEE>
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector getSubVector(int index, int n) {
checkIndex(index);
if (n < 0) {
throw new NotPositiveException(LocalizedFormats.NUMBER_OF_ELEMENTS_SHOULD_BE_POSITIVE, n);
}
checkIndex(index + n - 1);
OpenMapRealVector<SCANS>Distance(RealVector v) {
checkVectorDimensions(v.getDimension());
if (v instanceof OpenMapRealVector) {
return getLInfDistance((OpenMapRealVector) v);
} else {
return super.getLInfDistance(v);
}
}
/** {@inheritDoc} */
@Override
public boolean isInfinite() {
boolean infiniteFound = false;
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
final double value = iter.value();
if (Double.isNaN(value)) {
return false;
}
if (Double.isInfinite(value)) {
infiniteFound = true;
}
}
return infiniteFound;
}
/** {@inheritDoc} */
@Override
public boolean isNaN() {
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
if (Double.isNaN(iter.value())) {
return true;
}
}
return false;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector mapAdd(double d) {
return copy().mapAddToSelf(d);
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector mapAddToSelf(double d) {
for (int i = 0; i < virtualSize; i++) {
setEntry(i, getEntry(i) + d);
}
return this;
}
/** {@inheritDoc} */
@Override
public RealVector projection(RealVector v) {
checkVectorDimensions(v.getDimension());
return v.mapMultiply(dotProduct(v) / v.dotProduct(v));
}
/** {@inheritDoc} */
@Override
public void setEntry(int index, double value) {
checkIndex(index);
if (!isDefaultValue(value)) {
entries.put(index, value);
} else if (entries.containsKey(index)) {
entries.remove(index);
}
}
/** {@inheritDoc} */
@Override
public void setSubVector(int index, RealVector v) {
checkIndex(index);
checkIndex(index + v.getDimension() - 1);
for (int i = 0; i < v.getDimension(); i++) {
setEntry(i + index
Math, 29
<FILEB>
<CHANGES>
final int n = getDimension();
for (int i = 0; i < n; i++) {
res.setEntry(i, this.getEntry(i) / v.getEntry(i));
<CHANGEE>
<CHANGES>
if (v.isNaN() || v.isInfinite()) {
final int n = getDimension();
for (int i = 0; i < n; i++) {
final double y = v.getEntry(i);
if (Double.isNaN(y)) {
res.setEntry(i, Double.NaN);
} else if (Double.isInfinite(y)) {
final double x = this.getEntry(i);
res.setEntry(i, x * y);
}
}
}
<CHANGEE>
<FILEE>
<FILEB>
@Override
public double dotProduct(RealVector v) {
if(v instanceof OpenMapRealVector) {
return dotProduct((OpenMapRealVector)v);
} else {
return super.dotProduct(v);
}
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeDivide(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
/*
* MATH-803: it is not sufficient to loop through non zero entries of
* this only. Indeed, if this[i] = 0d and v[i] = 0d, then
* this[i] / v[i] = NaN, and not 0d.
*/
<CHANGES>
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() / v.getEntry(iter.key()));
<CHANGEE>
}
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeMultiply(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() * v.getEntry(iter.key()));
}
/*
* MATH-803: the above loop assumes that 0d * x = 0d for any double x,
* which allows to consider only the non-zero entries of this. However,
* this fails if this[i] == 0d and (v[i] = NaN or v[i] = Infinity).
*
* These special cases are handled below.
*/
<CHANGES>
<CHANGEE>
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector getSubVector(int index, int n) {
checkIndex(index);
if (n < 0) {
throw new NotPositiveException(LocalizedFormats.NUMBER_OF_ELEMENTS_SHOULD_BE_POSITIVE, n);
}
checkIndex(index + n - 1);
OpenMapRealVector<SCANS>, v.getEntry(i));
}
}
/** {@inheritDoc} */
@Override
public void set(double value) {
for (int i = 0; i < virtualSize; i++) {
setEntry(i, value);
}
}
/**
* Optimized method to subtract OpenMapRealVectors.
*
* @param v Vector to subtract from {@code this}.
* @return the difference of {@code this} and {@code v}.
* @throws org.apache.commons.math3.exception.DimensionMismatchException
* if the dimensions do not match.
*/
public OpenMapRealVector subtract(OpenMapRealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = copy();
Iterator iter = v.getEntries().iterator();
while (iter.hasNext()) {
iter.advance();
int key = iter.key();
if (entries.containsKey(key)) {
res.setEntry(key, entries.get(key) - iter.value());
} else {
res.setEntry(key, -iter.value());
}
}
return res;
}
/** {@inheritDoc} */
@Override
public RealVector subtract(RealVector v) {
checkVectorDimensions(v.getDimension());
if (v instanceof OpenMapRealVector) {
return subtract((OpenMapRealVector) v);
} else {
return super.subtract(v);
}
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector unitVector() {
OpenMapRealVector res = copy();
res.unitize();
return res;
}
/** {@inheritDoc} */
@Override
public void unitize() {
double norm = getNorm();
if (isDefaultValue(norm)) {
throw new MathArithmeticException(LocalizedFormats.ZERO_NORM);
}
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
entries.put(iter.key(), iter.value() / norm);
}
}
/** {@inheritDoc} */
@Override
public double[] toArray() {
double[] res = new double[virtualSize];
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res[iter.key()] = iter
Math, 29
<FILEB>
<CHANGES>
final int n = getDimension();
for (int i = 0; i < n; i++) {
res.setEntry(i, this.getEntry(i) / v.getEntry(i));
<CHANGEE>
<CHANGES>
if (v.isNaN() || v.isInfinite()) {
final int n = getDimension();
for (int i = 0; i < n; i++) {
final double y = v.getEntry(i);
if (Double.isNaN(y)) {
res.setEntry(i, Double.NaN);
} else if (Double.isInfinite(y)) {
final double x = this.getEntry(i);
res.setEntry(i, x * y);
}
}
}
<CHANGEE>
<FILEE>
<FILEB>
@Override
public double dotProduct(RealVector v) {
if(v instanceof OpenMapRealVector) {
return dotProduct((OpenMapRealVector)v);
} else {
return super.dotProduct(v);
}
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeDivide(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
/*
* MATH-803: it is not sufficient to loop through non zero entries of
* this only. Indeed, if this[i] = 0d and v[i] = 0d, then
* this[i] / v[i] = NaN, and not 0d.
*/
<CHANGES>
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() / v.getEntry(iter.key()));
<CHANGEE>
}
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeMultiply(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() * v.getEntry(iter.key()));
}
/*
* MATH-803: the above loop assumes that 0d * x = 0d for any double x,
* which allows to consider only the non-zero entries of this. However,
* this fails if this[i] == 0d and (v[i] = NaN or v[i] = Infinity).
*
* These special cases are handled below.
*/
<CHANGES>
<CHANGEE>
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector getSubVector(int index, int n) {
checkIndex(index);
if (n < 0) {
throw new NotPositiveException(LocalizedFormats.NUMBER_OF_ELEMENTS_SHOULD_BE_POSITIVE, n);
}
checkIndex(index + n - 1);
OpenMapRealVector<SCANS>.value();
}
return res;
}
/**
* {@inheritDoc}
* Implementation Note: This works on exact values, and as a result
* it is possible for {@code a.subtract(b)} to be the zero vector, while
* {@code a.hashCode() != b.hashCode()}.
*/
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
long temp;
temp = Double.doubleToLongBits(epsilon);
result = prime * result + (int) (temp ^ (temp >>> 32));
result = prime * result + virtualSize;
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
temp = Double.doubleToLongBits(iter.value());
result = prime * result + (int) (temp ^ (temp >>32));
}
return result;
}
/**
* {@inheritDoc}
* Implementation Note: This performs an exact comparison, and as a result
* it is possible for {@code a.subtract(b}} to be the zero vector, while
* {@code a.equals(b) == false}.
*/
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (!(obj instanceof OpenMapRealVector)) {
return false;
}
OpenMapRealVector other = (OpenMapRealVector) obj;
if (virtualSize != other.virtualSize) {
return false;
}
if (Double.doubleToLongBits(epsilon) !=
Double.doubleToLongBits(other.epsilon)) {
return false;
}
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
double test = other.getEntry(iter.key());
if (Double.doubleToLongBits(test) != Double.doubleToLongBits(iter.value())) {
return false;
}
}
iter = other.getEntries().iterator();
while (iter.hasNext()) {
iter.advance();
double test = iter.value();
if (Double.doubleToLongBits(test) != Double.doubleToLongBits(getEntry(iter.key()))) {
return false;
}
}
return true;
}
/**
Math, 29
<FILEB>
<CHANGES>
final int n = getDimension();
for (int i = 0; i < n; i++) {
res.setEntry(i, this.getEntry(i) / v.getEntry(i));
<CHANGEE>
<CHANGES>
if (v.isNaN() || v.isInfinite()) {
final int n = getDimension();
for (int i = 0; i < n; i++) {
final double y = v.getEntry(i);
if (Double.isNaN(y)) {
res.setEntry(i, Double.NaN);
} else if (Double.isInfinite(y)) {
final double x = this.getEntry(i);
res.setEntry(i, x * y);
}
}
}
<CHANGEE>
<FILEE>
<FILEB>
@Override
public double dotProduct(RealVector v) {
if(v instanceof OpenMapRealVector) {
return dotProduct((OpenMapRealVector)v);
} else {
return super.dotProduct(v);
}
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeDivide(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
/*
* MATH-803: it is not sufficient to loop through non zero entries of
* this only. Indeed, if this[i] = 0d and v[i] = 0d, then
* this[i] / v[i] = NaN, and not 0d.
*/
<CHANGES>
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() / v.getEntry(iter.key()));
<CHANGEE>
}
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeMultiply(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() * v.getEntry(iter.key()));
}
/*
* MATH-803: the above loop assumes that 0d * x = 0d for any double x,
* which allows to consider only the non-zero entries of this. However,
* this fails if this[i] == 0d and (v[i] = NaN or v[i] = Infinity).
*
* These special cases are handled below.
*/
<CHANGES>
<CHANGEE>
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector getSubVector(int index, int n) {
checkIndex(index);
if (n < 0) {
throw new NotPositiveException(LocalizedFormats.NUMBER_OF_ELEMENTS_SHOULD_BE_POSITIVE, n);
}
checkIndex(index + n - 1);
OpenMapRealVector<SCANS>
* V. Eijkhout, R. Pozo, C. Romine and H. Van der Vorst,
* <em>Templates for the Solution of Linear Systems: Building Blocks for
* Iterative Methods</em>, SIAM
* </dd>
* </dl>
*
* @version $Id$
* @since 3.0
*/
public abstract class RealLinearOperator {
/**
* Returns the dimension of the codomain of this operator.
*
* @return the number of rows of the underlying matrix
*/
public abstract int getRowDimension();
/**
* Returns the dimension of the domain of this operator.
*
* @return the number of columns of the underlying matrix
*/
public abstract int getColumnDimension();
/**
* Returns the result of multiplying {@code this} by the vector {@code x}.
*
* @param x the vector to operate on
* @return the product of {@code this} instance with {@code x}
* @throws org.apache.commons.math3.exception.DimensionMismatchException
* if the column dimension does not match the size of {@code x}
*/
public abstract RealVector operate(final RealVector x);
/**
* Returns the result of multiplying the transpose of {@code this} operator
* by the vector {@code x} (optional operation). The default implementation
* throws an {@link UnsupportedOperationException}. Users overriding this
* method must also override {@link #isTransposable()}.
*
* @param x the vector to operate on
* @return the product of the transpose of {@code this} instance with
* {@code x}
* @throws org.apache.commons.math3.exception.DimensionMismatchException
* if the row dimension does not match the size of {@code x}
* @throws UnsupportedOperationException if this operation is not supported
* by {@code this} operator
*/
public RealVector operateTranspose(final RealVector x)
throws DimensionMismatchException, UnsupportedOperationException {
throw new UnsupportedOperationException();
}
/**
* Returns {@code true} if this operator supports
* {@link #operateTranspose(RealVector)}. If {@code true} is returned,
* {@link #operateTranspose(RealVector)} should not throw
* {@code UnsupportedOperationException}. The default implementation returns
* {@code false}.
*
Math, 29
<FILEB>
<CHANGES>
final int n = getDimension();
for (int i = 0; i < n; i++) {
res.setEntry(i, this.getEntry(i) / v.getEntry(i));
<CHANGEE>
<CHANGES>
if (v.isNaN() || v.isInfinite()) {
final int n = getDimension();
for (int i = 0; i < n; i++) {
final double y = v.getEntry(i);
if (Double.isNaN(y)) {
res.setEntry(i, Double.NaN);
} else if (Double.isInfinite(y)) {
final double x = this.getEntry(i);
res.setEntry(i, x * y);
}
}
}
<CHANGEE>
<FILEE>
<FILEB>
@Override
public double dotProduct(RealVector v) {
if(v instanceof OpenMapRealVector) {
return dotProduct((OpenMapRealVector)v);
} else {
return super.dotProduct(v);
}
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeDivide(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
/*
* MATH-803: it is not sufficient to loop through non zero entries of
* this only. Indeed, if this[i] = 0d and v[i] = 0d, then
* this[i] / v[i] = NaN, and not 0d.
*/
<CHANGES>
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() / v.getEntry(iter.key()));
<CHANGEE>
}
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeMultiply(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() * v.getEntry(iter.key()));
}
/*
* MATH-803: the above loop assumes that 0d * x = 0d for any double x,
* which allows to consider only the non-zero entries of this. However,
* this fails if this[i] == 0d and (v[i] = NaN or v[i] = Infinity).
*
* These special cases are handled below.
*/
<CHANGES>
<CHANGEE>
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector getSubVector(int index, int n) {
checkIndex(index);
if (n < 0) {
throw new NotPositiveException(LocalizedFormats.NUMBER_OF_ELEMENTS_SHOULD_BE_POSITIVE, n);
}
checkIndex(index + n - 1);
OpenMapRealVector<SCANS>}.
* @throws org.apache.commons.math3.exception.DimensionMismatchException
* if {@code m} is not the same size as this matrix.
*/
public OpenMapRealMatrix subtract(OpenMapRealMatrix m) {
// Safety check.
MatrixUtils.checkAdditionCompatible(this, m);
final OpenMapRealMatrix out = new OpenMapRealMatrix(this);
for (OpenIntToDoubleHashMap.Iterator iterator = m.entries.iterator(); iterator.hasNext();) {
iterator.advance();
final int row = iterator.key() / columns;
final int col = iterator.key() - row * columns;
out.setEntry(row, col, getEntry(row, col) - iterator.value());
}
return out;
}
/** {@inheritDoc} */
@Override
public RealMatrix multiply(final RealMatrix m) {
try {
return multiply((OpenMapRealMatrix) m);
} catch (ClassCastException cce) {
// safety check
MatrixUtils.checkMultiplicationCompatible(this, m);
final int outCols = m.getColumnDimension();
final BlockRealMatrix out = new BlockRealMatrix(rows, outCols);
for (OpenIntToDoubleHashMap.Iterator iterator = entries.iterator(); iterator.hasNext();) {
iterator.advance();
final double value = iterator.value();
final int key = iterator.key();
final int i = key / columns;
final int k = key % columns;
for (int j = 0; j < outCols; ++j) {
out.addToEntry(i, j, value * m.getEntry(k, j));
}
}
return out;
}
}
/**
* Postmultiply this matrix by {@code m}.
*
* @param m Matrix to postmultiply by.
* @return {@code this} * {@code m}.
* @throws MatrixDimensionMismatchException
* if the number of rows of {@code m} differ from the number of columns
* of this matrix.
*/
public OpenMapRealMatrix multiply(OpenMapRealMatrix m) {
// Safety check.
MatrixUtils.checkMultiplicationCompatible(this, m);
final int outCols = m.getColumnDimension();
OpenMapRealMatrix out = new OpenMapRealMatrix(rows, out
Math, 29
<FILEB>
<CHANGES>
final int n = getDimension();
for (int i = 0; i < n; i++) {
res.setEntry(i, this.getEntry(i) / v.getEntry(i));
<CHANGEE>
<CHANGES>
if (v.isNaN() || v.isInfinite()) {
final int n = getDimension();
for (int i = 0; i < n; i++) {
final double y = v.getEntry(i);
if (Double.isNaN(y)) {
res.setEntry(i, Double.NaN);
} else if (Double.isInfinite(y)) {
final double x = this.getEntry(i);
res.setEntry(i, x * y);
}
}
}
<CHANGEE>
<FILEE>
<FILEB>
@Override
public double dotProduct(RealVector v) {
if(v instanceof OpenMapRealVector) {
return dotProduct((OpenMapRealVector)v);
} else {
return super.dotProduct(v);
}
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeDivide(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
/*
* MATH-803: it is not sufficient to loop through non zero entries of
* this only. Indeed, if this[i] = 0d and v[i] = 0d, then
* this[i] / v[i] = NaN, and not 0d.
*/
<CHANGES>
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() / v.getEntry(iter.key()));
<CHANGEE>
}
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeMultiply(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() * v.getEntry(iter.key()));
}
/*
* MATH-803: the above loop assumes that 0d * x = 0d for any double x,
* which allows to consider only the non-zero entries of this. However,
* this fails if this[i] == 0d and (v[i] = NaN or v[i] = Infinity).
*
* These special cases are handled below.
*/
<CHANGES>
<CHANGEE>
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector getSubVector(int index, int n) {
checkIndex(index);
if (n < 0) {
throw new NotPositiveException(LocalizedFormats.NUMBER_OF_ELEMENTS_SHOULD_BE_POSITIVE, n);
}
checkIndex(index + n - 1);
OpenMapRealVector<SCANS>Cols);
for (OpenIntToDoubleHashMap.Iterator iterator = entries.iterator(); iterator.hasNext();) {
iterator.advance();
final double value = iterator.value();
final int key = iterator.key();
final int i = key / columns;
final int k = key % columns;
for (int j = 0; j < outCols; ++j) {
final int rightKey = m.computeKey(k, j);
if (m.entries.containsKey(rightKey)) {
final int outKey = out.computeKey(i, j);
final double outValue =
out.entries.get(outKey) + value * m.entries.get(rightKey);
if (outValue == 0.0) {
out.entries.remove(outKey);
} else {
out.entries.put(outKey, outValue);
}
}
}
}
return out;
}
/** {@inheritDoc} */
@Override
public double getEntry(int row, int column) {
MatrixUtils.checkRowIndex(this, row);
MatrixUtils.checkColumnIndex(this, column);
return entries.get(computeKey(row, column));
}
/** {@inheritDoc} */
@Override
public int getRowDimension() {
return rows;
}
/** {@inheritDoc} */
@Override
public void setEntry(int row, int column, double value) {
MatrixUtils.checkRowIndex(this, row);
MatrixUtils.checkColumnIndex(this, column);
if (value == 0.0) {
entries.remove(computeKey(row, column));
} else {
entries.put(computeKey(row, column), value);
}
}
/** {@inheritDoc} */
@Override
public void addToEntry(int row, int column, double increment) {
MatrixUtils.checkRowIndex(this, row);
MatrixUtils.checkColumnIndex(this, column);
final int key = computeKey(row, column);
final double value = entries.get(key) + increment;
if (value == 0.0) {
entries.remove(key);
} else {
entries.put(key, value);
}
}
/** {@inheritDoc} */
@Override
public void multiplyEntry(
Math, 29
<FILEB>
<CHANGES>
final int n = getDimension();
for (int i = 0; i < n; i++) {
res.setEntry(i, this.getEntry(i) / v.getEntry(i));
<CHANGEE>
<CHANGES>
if (v.isNaN() || v.isInfinite()) {
final int n = getDimension();
for (int i = 0; i < n; i++) {
final double y = v.getEntry(i);
if (Double.isNaN(y)) {
res.setEntry(i, Double.NaN);
} else if (Double.isInfinite(y)) {
final double x = this.getEntry(i);
res.setEntry(i, x * y);
}
}
}
<CHANGEE>
<FILEE>
<FILEB>
@Override
public double dotProduct(RealVector v) {
if(v instanceof OpenMapRealVector) {
return dotProduct((OpenMapRealVector)v);
} else {
return super.dotProduct(v);
}
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeDivide(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
/*
* MATH-803: it is not sufficient to loop through non zero entries of
* this only. Indeed, if this[i] = 0d and v[i] = 0d, then
* this[i] / v[i] = NaN, and not 0d.
*/
<CHANGES>
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() / v.getEntry(iter.key()));
<CHANGEE>
}
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeMultiply(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() * v.getEntry(iter.key()));
}
/*
* MATH-803: the above loop assumes that 0d * x = 0d for any double x,
* which allows to consider only the non-zero entries of this. However,
* this fails if this[i] == 0d and (v[i] = NaN or v[i] = Infinity).
*
* These special cases are handled below.
*/
<CHANGES>
<CHANGEE>
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector getSubVector(int index, int n) {
checkIndex(index);
if (n < 0) {
throw new NotPositiveException(LocalizedFormats.NUMBER_OF_ELEMENTS_SHOULD_BE_POSITIVE, n);
}
checkIndex(index + n - 1);
OpenMapRealVector<SCANS> d) {
final int rowCount = getRowDimension();
final int columnCount = getColumnDimension();
final RealMatrix out = createMatrix(rowCount, columnCount);
for (int row = 0; row < rowCount; ++row) {
for (int col = 0; col < columnCount; ++col) {
out.setEntry(row, col, getEntry(row, col) * d);
}
}
return out;
}
/** {@inheritDoc} */
public RealMatrix multiply(final RealMatrix m) {
// Safety check.
MatrixUtils.checkMultiplicationCompatible(this, m);
final int nRows = getRowDimension();
final int nCols = m.getColumnDimension();
final int nSum = getColumnDimension();
final RealMatrix out = createMatrix(nRows, nCols);
for (int row = 0; row < nRows; ++row) {
for (int col = 0; col < nCols; ++col) {
double sum = 0;
for (int i = 0; i < nSum; ++i) {
sum += getEntry(row, i) * m.getEntry(i, col);
}
out.setEntry(row, col, sum);
}
}
return out;
}
/** {@inheritDoc} */
public RealMatrix preMultiply(final RealMatrix m) {
return m.multiply(this);
}
/** {@inheritDoc} */
public RealMatrix power(final int p) {
if (p < 0) {
throw new IllegalArgumentException("p must be >= 0");
}
if (!isSquare()) {
throw new NonSquareMatrixException(getRowDimension(), getColumnDimension());
}
if (p == 0) {
return MatrixUtils.createRealIdentityMatrix(this.getRowDimension());
}
if (p == 1) {
return this.copy();
}
final int power = p - 1;
/*
* Only log_2(p) operations is used by doing as follows:
* 5^214 = 5^128 * 5^64 * 5^16 * 5^4 * 5^2
*
* In general, the same approach is used for A^
Math, 29
<FILEB>
<CHANGES>
final int n = getDimension();
for (int i = 0; i < n; i++) {
res.setEntry(i, this.getEntry(i) / v.getEntry(i));
<CHANGEE>
<CHANGES>
if (v.isNaN() || v.isInfinite()) {
final int n = getDimension();
for (int i = 0; i < n; i++) {
final double y = v.getEntry(i);
if (Double.isNaN(y)) {
res.setEntry(i, Double.NaN);
} else if (Double.isInfinite(y)) {
final double x = this.getEntry(i);
res.setEntry(i, x * y);
}
}
}
<CHANGEE>
<FILEE>
<FILEB>
@Override
public double dotProduct(RealVector v) {
if(v instanceof OpenMapRealVector) {
return dotProduct((OpenMapRealVector)v);
} else {
return super.dotProduct(v);
}
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeDivide(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
/*
* MATH-803: it is not sufficient to loop through non zero entries of
* this only. Indeed, if this[i] = 0d and v[i] = 0d, then
* this[i] / v[i] = NaN, and not 0d.
*/
<CHANGES>
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() / v.getEntry(iter.key()));
<CHANGEE>
}
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeMultiply(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() * v.getEntry(iter.key()));
}
/*
* MATH-803: the above loop assumes that 0d * x = 0d for any double x,
* which allows to consider only the non-zero entries of this. However,
* this fails if this[i] == 0d and (v[i] = NaN or v[i] = Infinity).
*
* These special cases are handled below.
*/
<CHANGES>
<CHANGEE>
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector getSubVector(int index, int n) {
checkIndex(index);
if (n < 0) {
throw new NotPositiveException(LocalizedFormats.NUMBER_OF_ELEMENTS_SHOULD_BE_POSITIVE, n);
}
checkIndex(index + n - 1);
OpenMapRealVector<SCANS>p.
*/
final char[] binaryRepresentation = Integer.toBinaryString(power).toCharArray();
final ArrayList<Integer> nonZeroPositions = new ArrayList<Integer>();
int maxI = -1;
for (int i = 0; i < binaryRepresentation.length; ++i) {
if (binaryRepresentation[i] == '1') {
final int pos = binaryRepresentation.length - i - 1;
nonZeroPositions.add(pos);
// The positions are taken in turn, so maxI is only changed once
if (maxI == -1) {
maxI = pos;
}
}
}
RealMatrix[] results = new RealMatrix[maxI + 1];
results[0] = this.copy();
for (int i = 1; i <= maxI; ++i) {
results[i] = results[i-1].multiply(results[i-1]);
}
RealMatrix result = this.copy();
for (Integer i : nonZeroPositions) {
result = result.multiply(results[i]);
}
return result;
}
/** {@inheritDoc} */
public double[][] getData() {
final double[][] data = new double[getRowDimension()][getColumnDimension()];
for (int i = 0; i < data.length; ++i) {
final double[] dataI = data[i];
for (int j = 0; j < dataI.length; ++j) {
dataI[j] = getEntry(i, j);
}
}
return data;
}
/** {@inheritDoc} */
public double getNorm() {
return walkInColumnOrder(new RealMatrixPreservingVisitor() {
/** Last row index. */
private double endRow;
/** Sum of absolute values on one column. */
private double columnSum;
/** Maximal sum across all columns. */
private double maxColSum;
/** {@inheritDoc} */
public void start(final int rows, final int columns,
final int startRow, final int endRow,
final int startColumn, final int endColumn) {
this.endRow = endRow;
columnSum = 0;
maxColSum = 0;
}
/** {@inheritDoc} */
public void visit(final int row, final int
Math, 29
<FILEB>
<CHANGES>
final int n = getDimension();
for (int i = 0; i < n; i++) {
res.setEntry(i, this.getEntry(i) / v.getEntry(i));
<CHANGEE>
<CHANGES>
if (v.isNaN() || v.isInfinite()) {
final int n = getDimension();
for (int i = 0; i < n; i++) {
final double y = v.getEntry(i);
if (Double.isNaN(y)) {
res.setEntry(i, Double.NaN);
} else if (Double.isInfinite(y)) {
final double x = this.getEntry(i);
res.setEntry(i, x * y);
}
}
}
<CHANGEE>
<FILEE>
<FILEB>
@Override
public double dotProduct(RealVector v) {
if(v instanceof OpenMapRealVector) {
return dotProduct((OpenMapRealVector)v);
} else {
return super.dotProduct(v);
}
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeDivide(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
/*
* MATH-803: it is not sufficient to loop through non zero entries of
* this only. Indeed, if this[i] = 0d and v[i] = 0d, then
* this[i] / v[i] = NaN, and not 0d.
*/
<CHANGES>
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() / v.getEntry(iter.key()));
<CHANGEE>
}
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeMultiply(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() * v.getEntry(iter.key()));
}
/*
* MATH-803: the above loop assumes that 0d * x = 0d for any double x,
* which allows to consider only the non-zero entries of this. However,
* this fails if this[i] == 0d and (v[i] = NaN or v[i] = Infinity).
*
* These special cases are handled below.
*/
<CHANGES>
<CHANGEE>
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector getSubVector(int index, int n) {
checkIndex(index);
if (n < 0) {
throw new NotPositiveException(LocalizedFormats.NUMBER_OF_ELEMENTS_SHOULD_BE_POSITIVE, n);
}
checkIndex(index + n - 1);
OpenMapRealVector<SCANS> column, final double value) {
columnSum += FastMath.abs(value);
if (row == endRow) {
maxColSum = FastMath.max(maxColSum, columnSum);
columnSum = 0;
}
}
/** {@inheritDoc} */
public double end() {
return maxColSum;
}
});
}
/** {@inheritDoc} */
public double getFrobeniusNorm() {
return walkInOptimizedOrder(new RealMatrixPreservingVisitor() {
/** Sum of squared entries. */
private double sum;
/** {@inheritDoc} */
public void start(final int rows, final int columns,
final int startRow, final int endRow,
final int startColumn, final int endColumn) {
sum = 0;
}
/** {@inheritDoc} */
public void visit(final int row, final int column, final double value) {
sum += value * value;
}
/** {@inheritDoc} */
public double end() {
return FastMath.sqrt(sum);
}
});
}
/** {@inheritDoc} */
public RealMatrix getSubMatrix(final int startRow, final int endRow,
final int startColumn, final int endColumn) {
MatrixUtils.checkSubMatrixIndex(this, startRow, endRow, startColumn, endColumn);
final RealMatrix subMatrix =
createMatrix(endRow - startRow + 1, endColumn - startColumn + 1);
for (int i = startRow; i <= endRow; ++i) {
for (int j = startColumn; j <= endColumn; ++j) {
subMatrix.setEntry(i - startRow, j - startColumn, getEntry(i, j));
}
}
return subMatrix;
}
/** {@inheritDoc} */
public RealMatrix getSubMatrix(final int[] selectedRows, final int[] selectedColumns) {
// safety checks
MatrixUtils.checkSubMatrixIndex(this, selectedRows, selectedColumns);
// copy entries
final RealMatrix subMatrix =
createMatrix(selectedRows.length, selectedColumns.length);
subMatrix.walkInOptimizedOrder(new DefaultRealMatrixChangingVisitor() {
/** {@inheritDoc} */
@Override
public double visit(final int row, final int column, final double
Math, 29
<FILEB>
<CHANGES>
final int n = getDimension();
for (int i = 0; i < n; i++) {
res.setEntry(i, this.getEntry(i) / v.getEntry(i));
<CHANGEE>
<CHANGES>
if (v.isNaN() || v.isInfinite()) {
final int n = getDimension();
for (int i = 0; i < n; i++) {
final double y = v.getEntry(i);
if (Double.isNaN(y)) {
res.setEntry(i, Double.NaN);
} else if (Double.isInfinite(y)) {
final double x = this.getEntry(i);
res.setEntry(i, x * y);
}
}
}
<CHANGEE>
<FILEE>
<FILEB>
@Override
public double dotProduct(RealVector v) {
if(v instanceof OpenMapRealVector) {
return dotProduct((OpenMapRealVector)v);
} else {
return super.dotProduct(v);
}
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeDivide(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
/*
* MATH-803: it is not sufficient to loop through non zero entries of
* this only. Indeed, if this[i] = 0d and v[i] = 0d, then
* this[i] / v[i] = NaN, and not 0d.
*/
<CHANGES>
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() / v.getEntry(iter.key()));
<CHANGEE>
}
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeMultiply(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() * v.getEntry(iter.key()));
}
/*
* MATH-803: the above loop assumes that 0d * x = 0d for any double x,
* which allows to consider only the non-zero entries of this. However,
* this fails if this[i] == 0d and (v[i] = NaN or v[i] = Infinity).
*
* These special cases are handled below.
*/
<CHANGES>
<CHANGEE>
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector getSubVector(int index, int n) {
checkIndex(index);
if (n < 0) {
throw new NotPositiveException(LocalizedFormats.NUMBER_OF_ELEMENTS_SHOULD_BE_POSITIVE, n);
}
checkIndex(index + n - 1);
OpenMapRealVector<SCANS> value) {
return getEntry(selectedRows[row], selectedColumns[column]);
}
});
return subMatrix;
}
/** {@inheritDoc} */
public void copySubMatrix(final int startRow, final int endRow,
final int startColumn, final int endColumn,
final double[][] destination) {
// safety checks
MatrixUtils.checkSubMatrixIndex(this, startRow, endRow, startColumn, endColumn);
final int rowsCount = endRow + 1 - startRow;
final int columnsCount = endColumn + 1 - startColumn;
if ((destination.length < rowsCount) || (destination[0].length < columnsCount)) {
throw new MatrixDimensionMismatchException(destination.length, destination[0].length,
rowsCount, columnsCount);
}
// copy entries
walkInOptimizedOrder(new DefaultRealMatrixPreservingVisitor() {
/** Initial row index. */
private int startRow;
/** Initial column index. */
private int startColumn;
/** {@inheritDoc} */
@Override
public void start(final int rows, final int columns,
final int startRow, final int endRow,
final int startColumn, final int endColumn) {
this.startRow = startRow;
this.startColumn = startColumn;
}
/** {@inheritDoc} */
@Override
public void visit(final int row, final int column, final double value) {
destination[row - startRow][column - startColumn] = value;
}
}, startRow, endRow, startColumn, endColumn);
}
/** {@inheritDoc} */
public void copySubMatrix(int[] selectedRows, int[] selectedColumns, double[][] destination) {
// safety checks
MatrixUtils.checkSubMatrixIndex(this, selectedRows, selectedColumns);
if ((destination.length < selectedRows.length) ||
(destination[0].length < selectedColumns.length)) {
throw new MatrixDimensionMismatchException(destination.length, destination[0].length,
selectedRows.length, selectedColumns.length);
}
// copy entries
for (int i = 0; i < selectedRows.length; i++) {
final double[] destinationI = destination[i];
for (int j = 0; j < selectedColumns.length; j++)
Math, 29
<FILEB>
<CHANGES>
final int n = getDimension();
for (int i = 0; i < n; i++) {
res.setEntry(i, this.getEntry(i) / v.getEntry(i));
<CHANGEE>
<CHANGES>
if (v.isNaN() || v.isInfinite()) {
final int n = getDimension();
for (int i = 0; i < n; i++) {
final double y = v.getEntry(i);
if (Double.isNaN(y)) {
res.setEntry(i, Double.NaN);
} else if (Double.isInfinite(y)) {
final double x = this.getEntry(i);
res.setEntry(i, x * y);
}
}
}
<CHANGEE>
<FILEE>
<FILEB>
@Override
public double dotProduct(RealVector v) {
if(v instanceof OpenMapRealVector) {
return dotProduct((OpenMapRealVector)v);
} else {
return super.dotProduct(v);
}
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeDivide(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
/*
* MATH-803: it is not sufficient to loop through non zero entries of
* this only. Indeed, if this[i] = 0d and v[i] = 0d, then
* this[i] / v[i] = NaN, and not 0d.
*/
<CHANGES>
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() / v.getEntry(iter.key()));
<CHANGEE>
}
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeMultiply(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() * v.getEntry(iter.key()));
}
/*
* MATH-803: the above loop assumes that 0d * x = 0d for any double x,
* which allows to consider only the non-zero entries of this. However,
* this fails if this[i] == 0d and (v[i] = NaN or v[i] = Infinity).
*
* These special cases are handled below.
*/
<CHANGES>
<CHANGEE>
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector getSubVector(int index, int n) {
checkIndex(index);
if (n < 0) {
throw new NotPositiveException(LocalizedFormats.NUMBER_OF_ELEMENTS_SHOULD_BE_POSITIVE, n);
}
checkIndex(index + n - 1);
OpenMapRealVector<SCANS> {
destinationI[j] = getEntry(selectedRows[i], selectedColumns[j]);
}
}
}
/** {@inheritDoc} */
public void setSubMatrix(final double[][] subMatrix, final int row, final int column)
throws NoDataException, DimensionMismatchException, NullArgumentException {
MathUtils.checkNotNull(subMatrix);
final int nRows = subMatrix.length;
if (nRows == 0) {
throw new NoDataException(LocalizedFormats.AT_LEAST_ONE_ROW);
}
final int nCols = subMatrix[0].length;
if (nCols == 0) {
throw new NoDataException(LocalizedFormats.AT_LEAST_ONE_COLUMN);
}
for (int r = 1; r < nRows; ++r) {
if (subMatrix[r].length != nCols) {
throw new DimensionMismatchException(nCols, subMatrix[r].length);
}
}
MatrixUtils.checkRowIndex(this, row);
MatrixUtils.checkColumnIndex(this, column);
MatrixUtils.checkRowIndex(this, nRows + row - 1);
MatrixUtils.checkColumnIndex(this, nCols + column - 1);
for (int i = 0; i < nRows; ++i) {
for (int j = 0; j < nCols; ++j) {
setEntry(row + i, column + j, subMatrix[i][j]);
}
}
}
/** {@inheritDoc} */
public RealMatrix getRowMatrix(final int row) {
MatrixUtils.checkRowIndex(this, row);
final int nCols = getColumnDimension();
final RealMatrix out = createMatrix(1, nCols);
for (int i = 0; i < nCols; ++i) {
out.setEntry(0, i, getEntry(row, i));
}
return out;
}
/** {@inheritDoc} */
public void setRowMatrix(final int row, final RealMatrix matrix) {
MatrixUtils.checkRowIndex(this, row);
final int nCols = getColumnDimension();
if ((matrix.getRowDimension() != 1) ||
(matrix.getColumnDimension() != nCols)) {
throw new MatrixDimensionMismatchException(matrix.getRowDimension
Math, 29
<FILEB>
<CHANGES>
final int n = getDimension();
for (int i = 0; i < n; i++) {
res.setEntry(i, this.getEntry(i) / v.getEntry(i));
<CHANGEE>
<CHANGES>
if (v.isNaN() || v.isInfinite()) {
final int n = getDimension();
for (int i = 0; i < n; i++) {
final double y = v.getEntry(i);
if (Double.isNaN(y)) {
res.setEntry(i, Double.NaN);
} else if (Double.isInfinite(y)) {
final double x = this.getEntry(i);
res.setEntry(i, x * y);
}
}
}
<CHANGEE>
<FILEE>
<FILEB>
@Override
public double dotProduct(RealVector v) {
if(v instanceof OpenMapRealVector) {
return dotProduct((OpenMapRealVector)v);
} else {
return super.dotProduct(v);
}
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeDivide(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
/*
* MATH-803: it is not sufficient to loop through non zero entries of
* this only. Indeed, if this[i] = 0d and v[i] = 0d, then
* this[i] / v[i] = NaN, and not 0d.
*/
<CHANGES>
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() / v.getEntry(iter.key()));
<CHANGEE>
}
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeMultiply(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() * v.getEntry(iter.key()));
}
/*
* MATH-803: the above loop assumes that 0d * x = 0d for any double x,
* which allows to consider only the non-zero entries of this. However,
* this fails if this[i] == 0d and (v[i] = NaN or v[i] = Infinity).
*
* These special cases are handled below.
*/
<CHANGES>
<CHANGEE>
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector getSubVector(int index, int n) {
checkIndex(index);
if (n < 0) {
throw new NotPositiveException(LocalizedFormats.NUMBER_OF_ELEMENTS_SHOULD_BE_POSITIVE, n);
}
checkIndex(index + n - 1);
OpenMapRealVector<SCANS>(),
matrix.getColumnDimension(),
1, nCols);
}
for (int i = 0; i < nCols; ++i) {
setEntry(row, i, matrix.getEntry(0, i));
}
}
/** {@inheritDoc} */
public RealMatrix getColumnMatrix(final int column) {
MatrixUtils.checkColumnIndex(this, column);
final int nRows = getRowDimension();
final RealMatrix out = createMatrix(nRows, 1);
for (int i = 0; i < nRows; ++i) {
out.setEntry(i, 0, getEntry(i, column));
}
return out;
}
/** {@inheritDoc} */
public void setColumnMatrix(final int column, final RealMatrix matrix) {
MatrixUtils.checkColumnIndex(this, column);
final int nRows = getRowDimension();
if ((matrix.getRowDimension() != nRows) ||
(matrix.getColumnDimension() != 1)) {
throw new MatrixDimensionMismatchException(matrix.getRowDimension(),
matrix.getColumnDimension(),
nRows, 1);
}
for (int i = 0; i < nRows; ++i) {
setEntry(i, column, matrix.getEntry(i, 0));
}
}
/** {@inheritDoc} */
public RealVector getRowVector(final int row) {
return new ArrayRealVector(getRow(row), false);
}
/** {@inheritDoc} */
public void setRowVector(final int row, final RealVector vector) {
MatrixUtils.checkRowIndex(this, row);
final int nCols = getColumnDimension();
if (vector.getDimension() != nCols) {
throw new MatrixDimensionMismatchException(1, vector.getDimension(),
1, nCols);
}
for (int i = 0; i < nCols; ++i) {
setEntry(row, i, vector.getEntry(i));
}
}
/** {@inheritDoc} */
public RealVector getColumnVector(final int column) {
return new ArrayRealVector(getColumn(column), false);
}
/** {@inheritDoc} */
public void setColumnVector(final int column, final RealVector vector) {
MatrixUtils.checkColumnIndex(this, column);
Math, 29
<FILEB>
<CHANGES>
final int n = getDimension();
for (int i = 0; i < n; i++) {
res.setEntry(i, this.getEntry(i) / v.getEntry(i));
<CHANGEE>
<CHANGES>
if (v.isNaN() || v.isInfinite()) {
final int n = getDimension();
for (int i = 0; i < n; i++) {
final double y = v.getEntry(i);
if (Double.isNaN(y)) {
res.setEntry(i, Double.NaN);
} else if (Double.isInfinite(y)) {
final double x = this.getEntry(i);
res.setEntry(i, x * y);
}
}
}
<CHANGEE>
<FILEE>
<FILEB>
@Override
public double dotProduct(RealVector v) {
if(v instanceof OpenMapRealVector) {
return dotProduct((OpenMapRealVector)v);
} else {
return super.dotProduct(v);
}
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeDivide(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
/*
* MATH-803: it is not sufficient to loop through non zero entries of
* this only. Indeed, if this[i] = 0d and v[i] = 0d, then
* this[i] / v[i] = NaN, and not 0d.
*/
<CHANGES>
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() / v.getEntry(iter.key()));
<CHANGEE>
}
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeMultiply(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() * v.getEntry(iter.key()));
}
/*
* MATH-803: the above loop assumes that 0d * x = 0d for any double x,
* which allows to consider only the non-zero entries of this. However,
* this fails if this[i] == 0d and (v[i] = NaN or v[i] = Infinity).
*
* These special cases are handled below.
*/
<CHANGES>
<CHANGEE>
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector getSubVector(int index, int n) {
checkIndex(index);
if (n < 0) {
throw new NotPositiveException(LocalizedFormats.NUMBER_OF_ELEMENTS_SHOULD_BE_POSITIVE, n);
}
checkIndex(index + n - 1);
OpenMapRealVector<SCANS>
final int nRows = getRowDimension();
if (vector.getDimension() != nRows) {
throw new MatrixDimensionMismatchException(vector.getDimension(), 1,
nRows, 1);
}
for (int i = 0; i < nRows; ++i) {
setEntry(i, column, vector.getEntry(i));
}
}
/** {@inheritDoc} */
public double[] getRow(final int row) {
MatrixUtils.checkRowIndex(this, row);
final int nCols = getColumnDimension();
final double[] out = new double[nCols];
for (int i = 0; i < nCols; ++i) {
out[i] = getEntry(row, i);
}
return out;
}
/** {@inheritDoc} */
public void setRow(final int row, final double[] array) {
MatrixUtils.checkRowIndex(this, row);
final int nCols = getColumnDimension();
if (array.length != nCols) {
throw new MatrixDimensionMismatchException(1, array.length, 1, nCols);
}
for (int i = 0; i < nCols; ++i) {
setEntry(row, i, array[i]);
}
}
/** {@inheritDoc} */
public double[] getColumn(final int column) {
MatrixUtils.checkColumnIndex(this, column);
final int nRows = getRowDimension();
final double[] out = new double[nRows];
for (int i = 0; i < nRows; ++i) {
out[i] = getEntry(i, column);
}
return out;
}
/** {@inheritDoc} */
public void setColumn(final int column, final double[] array) {
MatrixUtils.checkColumnIndex(this, column);
final int nRows = getRowDimension();
if (array.length != nRows) {
throw new MatrixDimensionMismatchException(array.length, 1, nRows, 1);
}
for (int i = 0; i < nRows; ++i) {
setEntry(i, column, array[i]);
}
}
/** {@inheritDoc} */
public abstract double getEntry(int row, int column);
/** {@
Math, 29
<FILEB>
<CHANGES>
final int n = getDimension();
for (int i = 0; i < n; i++) {
res.setEntry(i, this.getEntry(i) / v.getEntry(i));
<CHANGEE>
<CHANGES>
if (v.isNaN() || v.isInfinite()) {
final int n = getDimension();
for (int i = 0; i < n; i++) {
final double y = v.getEntry(i);
if (Double.isNaN(y)) {
res.setEntry(i, Double.NaN);
} else if (Double.isInfinite(y)) {
final double x = this.getEntry(i);
res.setEntry(i, x * y);
}
}
}
<CHANGEE>
<FILEE>
<FILEB>
@Override
public double dotProduct(RealVector v) {
if(v instanceof OpenMapRealVector) {
return dotProduct((OpenMapRealVector)v);
} else {
return super.dotProduct(v);
}
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeDivide(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
/*
* MATH-803: it is not sufficient to loop through non zero entries of
* this only. Indeed, if this[i] = 0d and v[i] = 0d, then
* this[i] / v[i] = NaN, and not 0d.
*/
<CHANGES>
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() / v.getEntry(iter.key()));
<CHANGEE>
}
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeMultiply(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() * v.getEntry(iter.key()));
}
/*
* MATH-803: the above loop assumes that 0d * x = 0d for any double x,
* which allows to consider only the non-zero entries of this. However,
* this fails if this[i] == 0d and (v[i] = NaN or v[i] = Infinity).
*
* These special cases are handled below.
*/
<CHANGES>
<CHANGEE>
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector getSubVector(int index, int n) {
checkIndex(index);
if (n < 0) {
throw new NotPositiveException(LocalizedFormats.NUMBER_OF_ELEMENTS_SHOULD_BE_POSITIVE, n);
}
checkIndex(index + n - 1);
OpenMapRealVector<SCANS>inheritDoc} */
public abstract void setEntry(int row, int column, double value);
/** {@inheritDoc} */
public void addToEntry(int row, int column, double increment) {
MatrixUtils.checkMatrixIndex(this, row, column);
setEntry(row, column, getEntry(row, column) + increment);
}
/** {@inheritDoc} */
public void multiplyEntry(int row, int column, double factor) {
MatrixUtils.checkMatrixIndex(this, row, column);
setEntry(row, column, getEntry(row, column) * factor);
}
/** {@inheritDoc} */
public RealMatrix transpose() {
final int nRows = getRowDimension();
final int nCols = getColumnDimension();
final RealMatrix out = createMatrix(nCols, nRows);
walkInOptimizedOrder(new DefaultRealMatrixPreservingVisitor() {
/** {@inheritDoc} */
@Override
public void visit(final int row, final int column, final double value) {
out.setEntry(column, row, value);
}
});
return out;
}
/** {@inheritDoc} */
public boolean isSquare() {
return getColumnDimension() == getRowDimension();
}
/** {@inheritDoc} */
/**
* Returns the number of rows of this matrix.
*
* @return the number of rows.
*/
@Override
public abstract int getRowDimension();
/**
* Returns the number of columns of this matrix.
*
* @return the number of columns.
*/
@Override
public abstract int getColumnDimension();
/** {@inheritDoc} */
public double getTrace() {
final int nRows = getRowDimension();
final int nCols = getColumnDimension();
if (nRows != nCols) {
throw new NonSquareMatrixException(nRows, nCols);
}
double trace = 0;
for (int i = 0; i < nRows; ++i) {
trace += getEntry(i, i);
}
return trace;
}
/** {@inheritDoc} */
public double[] operate(final double[] v) {
final int nRows = getRowDimension();
final int nCols = getColumnDimension();
if (v.length != nCols) {
throw new DimensionMismatchException(v
Math, 29
<FILEB>
<CHANGES>
final int n = getDimension();
for (int i = 0; i < n; i++) {
res.setEntry(i, this.getEntry(i) / v.getEntry(i));
<CHANGEE>
<CHANGES>
if (v.isNaN() || v.isInfinite()) {
final int n = getDimension();
for (int i = 0; i < n; i++) {
final double y = v.getEntry(i);
if (Double.isNaN(y)) {
res.setEntry(i, Double.NaN);
} else if (Double.isInfinite(y)) {
final double x = this.getEntry(i);
res.setEntry(i, x * y);
}
}
}
<CHANGEE>
<FILEE>
<FILEB>
@Override
public double dotProduct(RealVector v) {
if(v instanceof OpenMapRealVector) {
return dotProduct((OpenMapRealVector)v);
} else {
return super.dotProduct(v);
}
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeDivide(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
/*
* MATH-803: it is not sufficient to loop through non zero entries of
* this only. Indeed, if this[i] = 0d and v[i] = 0d, then
* this[i] / v[i] = NaN, and not 0d.
*/
<CHANGES>
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() / v.getEntry(iter.key()));
<CHANGEE>
}
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeMultiply(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() * v.getEntry(iter.key()));
}
/*
* MATH-803: the above loop assumes that 0d * x = 0d for any double x,
* which allows to consider only the non-zero entries of this. However,
* this fails if this[i] == 0d and (v[i] = NaN or v[i] = Infinity).
*
* These special cases are handled below.
*/
<CHANGES>
<CHANGEE>
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector getSubVector(int index, int n) {
checkIndex(index);
if (n < 0) {
throw new NotPositiveException(LocalizedFormats.NUMBER_OF_ELEMENTS_SHOULD_BE_POSITIVE, n);
}
checkIndex(index + n - 1);
OpenMapRealVector<SCANS>.length, nCols);
}
final double[] out = new double[nRows];
for (int row = 0; row < nRows; ++row) {
double sum = 0;
for (int i = 0; i < nCols; ++i) {
sum += getEntry(row, i) * v[i];
}
out[row] = sum;
}
return out;
}
/** {@inheritDoc} */
@Override
public RealVector operate(final RealVector v) {
try {
return new ArrayRealVector(operate(((ArrayRealVector) v).getDataRef()), false);
} catch (ClassCastException cce) {
final int nRows = getRowDimension();
final int nCols = getColumnDimension();
if (v.getDimension() != nCols) {
throw new DimensionMismatchException(v.getDimension(), nCols);
}
final double[] out = new double[nRows];
for (int row = 0; row < nRows; ++row) {
double sum = 0;
for (int i = 0; i < nCols; ++i) {
sum += getEntry(row, i) * v.getEntry(i);
}
out[row] = sum;
}
return new ArrayRealVector(out, false);
}
}
/** {@inheritDoc} */
public double[] preMultiply(final double[] v) {
final int nRows = getRowDimension();
final int nCols = getColumnDimension();
if (v.length != nRows) {
throw new DimensionMismatchException(v.length, nRows);
}
final double[] out = new double[nCols];
for (int col = 0; col < nCols; ++col) {
double sum = 0;
for (int i = 0; i < nRows; ++i) {
sum += getEntry(i, col) * v[i];
}
out[col] = sum;
}
return out;
}
/** {@inheritDoc} */
public RealVector preMultiply(final RealVector v) {
try {
return new ArrayRealVector(preMultiply(((ArrayRealVector) v).getDataRef()), false);
} catch (ClassCastException cce) {
final int
Math, 29
<FILEB>
<CHANGES>
final int n = getDimension();
for (int i = 0; i < n; i++) {
res.setEntry(i, this.getEntry(i) / v.getEntry(i));
<CHANGEE>
<CHANGES>
if (v.isNaN() || v.isInfinite()) {
final int n = getDimension();
for (int i = 0; i < n; i++) {
final double y = v.getEntry(i);
if (Double.isNaN(y)) {
res.setEntry(i, Double.NaN);
} else if (Double.isInfinite(y)) {
final double x = this.getEntry(i);
res.setEntry(i, x * y);
}
}
}
<CHANGEE>
<FILEE>
<FILEB>
@Override
public double dotProduct(RealVector v) {
if(v instanceof OpenMapRealVector) {
return dotProduct((OpenMapRealVector)v);
} else {
return super.dotProduct(v);
}
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeDivide(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
/*
* MATH-803: it is not sufficient to loop through non zero entries of
* this only. Indeed, if this[i] = 0d and v[i] = 0d, then
* this[i] / v[i] = NaN, and not 0d.
*/
<CHANGES>
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() / v.getEntry(iter.key()));
<CHANGEE>
}
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeMultiply(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() * v.getEntry(iter.key()));
}
/*
* MATH-803: the above loop assumes that 0d * x = 0d for any double x,
* which allows to consider only the non-zero entries of this. However,
* this fails if this[i] == 0d and (v[i] = NaN or v[i] = Infinity).
*
* These special cases are handled below.
*/
<CHANGES>
<CHANGEE>
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector getSubVector(int index, int n) {
checkIndex(index);
if (n < 0) {
throw new NotPositiveException(LocalizedFormats.NUMBER_OF_ELEMENTS_SHOULD_BE_POSITIVE, n);
}
checkIndex(index + n - 1);
OpenMapRealVector<SCANS> nRows = getRowDimension();
final int nCols = getColumnDimension();
if (v.getDimension() != nRows) {
throw new DimensionMismatchException(v.getDimension(), nRows);
}
final double[] out = new double[nCols];
for (int col = 0; col < nCols; ++col) {
double sum = 0;
for (int i = 0; i < nRows; ++i) {
sum += getEntry(i, col) * v.getEntry(i);
}
out[col] = sum;
}
return new ArrayRealVector(out, false);
}
}
/** {@inheritDoc} */
public double walkInRowOrder(final RealMatrixChangingVisitor visitor) {
final int rows = getRowDimension();
final int columns = getColumnDimension();
visitor.start(rows, columns, 0, rows - 1, 0, columns - 1);
for (int row = 0; row < rows; ++row) {
for (int column = 0; column < columns; ++column) {
final double oldValue = getEntry(row, column);
final double newValue = visitor.visit(row, column, oldValue);
setEntry(row, column, newValue);
}
}
return visitor.end();
}
/** {@inheritDoc} */
public double walkInRowOrder(final RealMatrixPreservingVisitor visitor) {
final int rows = getRowDimension();
final int columns = getColumnDimension();
visitor.start(rows, columns, 0, rows - 1, 0, columns - 1);
for (int row = 0; row < rows; ++row) {
for (int column = 0; column < columns; ++column) {
visitor.visit(row, column, getEntry(row, column));
}
}
return visitor.end();
}
/** {@inheritDoc} */
public double walkInRowOrder(final RealMatrixChangingVisitor visitor,
final int startRow, final int endRow,
final int startColumn, final int endColumn) {
MatrixUtils.checkSubMatrixIndex(this, startRow, endRow, startColumn, endColumn);
visitor.start(getRowDimension(), getColumnDimension(),
startRow, endRow
Math, 29
<FILEB>
<CHANGES>
final int n = getDimension();
for (int i = 0; i < n; i++) {
res.setEntry(i, this.getEntry(i) / v.getEntry(i));
<CHANGEE>
<CHANGES>
if (v.isNaN() || v.isInfinite()) {
final int n = getDimension();
for (int i = 0; i < n; i++) {
final double y = v.getEntry(i);
if (Double.isNaN(y)) {
res.setEntry(i, Double.NaN);
} else if (Double.isInfinite(y)) {
final double x = this.getEntry(i);
res.setEntry(i, x * y);
}
}
}
<CHANGEE>
<FILEE>
<FILEB>
@Override
public double dotProduct(RealVector v) {
if(v instanceof OpenMapRealVector) {
return dotProduct((OpenMapRealVector)v);
} else {
return super.dotProduct(v);
}
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeDivide(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
/*
* MATH-803: it is not sufficient to loop through non zero entries of
* this only. Indeed, if this[i] = 0d and v[i] = 0d, then
* this[i] / v[i] = NaN, and not 0d.
*/
<CHANGES>
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() / v.getEntry(iter.key()));
<CHANGEE>
}
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeMultiply(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() * v.getEntry(iter.key()));
}
/*
* MATH-803: the above loop assumes that 0d * x = 0d for any double x,
* which allows to consider only the non-zero entries of this. However,
* this fails if this[i] == 0d and (v[i] = NaN or v[i] = Infinity).
*
* These special cases are handled below.
*/
<CHANGES>
<CHANGEE>
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector getSubVector(int index, int n) {
checkIndex(index);
if (n < 0) {
throw new NotPositiveException(LocalizedFormats.NUMBER_OF_ELEMENTS_SHOULD_BE_POSITIVE, n);
}
checkIndex(index + n - 1);
OpenMapRealVector<SCANS>Visitor visitor,
final int startRow, final int endRow,
final int startColumn, final int endColumn) {
return walkInRowOrder(visitor, startRow, endRow, startColumn, endColumn);
}
/**
* Get a string representation for this matrix.
* @return a string representation for this matrix
*/
@Override
public String toString() {
final int nRows = getRowDimension();
final int nCols = getColumnDimension();
final StringBuffer res = new StringBuffer();
String fullClassName = getClass().getName();
String shortClassName = fullClassName.substring(fullClassName.lastIndexOf('.') + 1);
res.append(shortClassName).append("{");
for (int i = 0; i < nRows; ++i) {
if (i > 0) {
res.append(",");
}
res.append("{");
for (int j = 0; j < nCols; ++j) {
if (j > 0) {
res.append(",");
}
res.append(getEntry(i, j));
}
res.append("}");
}
res.append("}");
return res.toString();
}
/**
* Returns true iff <code>object</code> is a
* <code>RealMatrix</code> instance with the same dimensions as this
* and all corresponding matrix entries are equal.
*
* @param object the object to test equality against.
* @return true if object equals this
*/
@Override
public boolean equals(final Object object) {
if (object == this ) {
return true;
}
if (object instanceof RealMatrix == false) {
return false;
}
RealMatrix m = (RealMatrix) object;
final int nRows = getRowDimension();
final int nCols = getColumnDimension();
if (m.getColumnDimension() != nCols || m.getRowDimension() != nRows) {
return false;
}
for (int row = 0; row < nRows; ++row) {
for (int col = 0; col < nCols; ++col) {
if (getEntry(row, col) != m.getEntry(row, col)) {
return false;
}
}
}
return true;
}
/**
* Computes a hashcode for
Math, 29
<FILEB>
<CHANGES>
final int n = getDimension();
for (int i = 0; i < n; i++) {
res.setEntry(i, this.getEntry(i) / v.getEntry(i));
<CHANGEE>
<CHANGES>
if (v.isNaN() || v.isInfinite()) {
final int n = getDimension();
for (int i = 0; i < n; i++) {
final double y = v.getEntry(i);
if (Double.isNaN(y)) {
res.setEntry(i, Double.NaN);
} else if (Double.isInfinite(y)) {
final double x = this.getEntry(i);
res.setEntry(i, x * y);
}
}
}
<CHANGEE>
<FILEE>
<FILEB>
@Override
public double dotProduct(RealVector v) {
if(v instanceof OpenMapRealVector) {
return dotProduct((OpenMapRealVector)v);
} else {
return super.dotProduct(v);
}
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeDivide(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
/*
* MATH-803: it is not sufficient to loop through non zero entries of
* this only. Indeed, if this[i] = 0d and v[i] = 0d, then
* this[i] / v[i] = NaN, and not 0d.
*/
<CHANGES>
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() / v.getEntry(iter.key()));
<CHANGEE>
}
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeMultiply(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() * v.getEntry(iter.key()));
}
/*
* MATH-803: the above loop assumes that 0d * x = 0d for any double x,
* which allows to consider only the non-zero entries of this. However,
* this fails if this[i] == 0d and (v[i] = NaN or v[i] = Infinity).
*
* These special cases are handled below.
*/
<CHANGES>
<CHANGEE>
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector getSubVector(int index, int n) {
checkIndex(index);
if (n < 0) {
throw new NotPositiveException(LocalizedFormats.NUMBER_OF_ELEMENTS_SHOULD_BE_POSITIVE, n);
}
checkIndex(index + n - 1);
OpenMapRealVector<SCANS> to append to this one.
* @return a new vector.
*/
public abstract RealVector append(RealVector v);
/**
* Construct a new vector by appending a double to this vector.
*
* @param d double to append.
* @return a new vector.
*/
public abstract RealVector append(double d);
/**
* Get a subvector from consecutive elements.
*
* @param index index of first element.
* @param n number of elements to be retrieved.
* @return a vector containing n elements.
* @throws org.apache.commons.math3.exception.OutOfRangeException
* if the index is not valid.
* @throws org.apache.commons.math3.exception.NotPositiveException
* if the number of elements is not positive
*/
public abstract RealVector getSubVector(int index, int n);
/**
* Set a sequence of consecutive elements.
*
* @param index index of first element to be set.
* @param v vector containing the values to set.
* @throws org.apache.commons.math3.exception.OutOfRangeException
* if the index is not valid.
*/
public abstract void setSubVector(int index, RealVector v);
/**
* Check whether any coordinate of this vector is {@code NaN}.
*
* @return {@code true} if any coordinate of this vector is {@code NaN},
* {@code false} otherwise.
*/
public abstract boolean isNaN();
/**
* Check whether any coordinate of this vector is infinite and none are {@code NaN}.
*
* @return {@code true} if any coordinate of this vector is infinite and
* none are {@code NaN}, {@code false} otherwise.
*/
public abstract boolean isInfinite();
/**
* Check if instance and specified vectors have the same dimension.
*
* @param v Vector to compare instance with.
* @throws DimensionMismatchException if the vectors do not
* have the same dimension.
*/
protected void checkVectorDimensions(RealVector v) {
checkVectorDimensions(v.getDimension());
}
/**
* Check if instance dimension is equal to some expected value.
*
* @param n Expected dimension.
* @throws DimensionMismatchException if the dimension is
* inconsistent with the vector size.
*/
protected void
Math, 29
<FILEB>
<CHANGES>
final int n = getDimension();
for (int i = 0; i < n; i++) {
res.setEntry(i, this.getEntry(i) / v.getEntry(i));
<CHANGEE>
<CHANGES>
if (v.isNaN() || v.isInfinite()) {
final int n = getDimension();
for (int i = 0; i < n; i++) {
final double y = v.getEntry(i);
if (Double.isNaN(y)) {
res.setEntry(i, Double.NaN);
} else if (Double.isInfinite(y)) {
final double x = this.getEntry(i);
res.setEntry(i, x * y);
}
}
}
<CHANGEE>
<FILEE>
<FILEB>
@Override
public double dotProduct(RealVector v) {
if(v instanceof OpenMapRealVector) {
return dotProduct((OpenMapRealVector)v);
} else {
return super.dotProduct(v);
}
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeDivide(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
/*
* MATH-803: it is not sufficient to loop through non zero entries of
* this only. Indeed, if this[i] = 0d and v[i] = 0d, then
* this[i] / v[i] = NaN, and not 0d.
*/
<CHANGES>
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() / v.getEntry(iter.key()));
<CHANGEE>
}
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeMultiply(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() * v.getEntry(iter.key()));
}
/*
* MATH-803: the above loop assumes that 0d * x = 0d for any double x,
* which allows to consider only the non-zero entries of this. However,
* this fails if this[i] == 0d and (v[i] = NaN or v[i] = Infinity).
*
* These special cases are handled below.
*/
<CHANGES>
<CHANGEE>
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector getSubVector(int index, int n) {
checkIndex(index);
if (n < 0) {
throw new NotPositiveException(LocalizedFormats.NUMBER_OF_ELEMENTS_SHOULD_BE_POSITIVE, n);
}
checkIndex(index + n - 1);
OpenMapRealVector<SCANS>.abs(e.getValue() - v.getEntry(e.getIndex())), d);
}
return d;
}
/**
* Get the index of the minimum entry.
*
* @return the index of the minimum entry or -1 if vector length is 0
* or all entries are {@code NaN}.
*/
public int getMinIndex() {
int minIndex = -1;
double minValue = Double.POSITIVE_INFINITY;
Iterator<Entry> iterator = iterator();
while (iterator.hasNext()) {
final Entry entry = iterator.next();
if (entry.getValue() <= minValue) {
minIndex = entry.getIndex();
minValue = entry.getValue();
}
}
return minIndex;
}
/**
* Get the value of the minimum entry.
*
* @return the value of the minimum entry or {@code NaN} if all
* entries are {@code NaN}.
*/
public double getMinValue() {
final int minIndex = getMinIndex();
return minIndex < 0 ? Double.NaN : getEntry(minIndex);
}
/**
* Get the index of the maximum entry.
*
* @return the index of the maximum entry or -1 if vector length is 0
* or all entries are {@code NaN}
*/
public int getMaxIndex() {
int maxIndex = -1;
double maxValue = Double.NEGATIVE_INFINITY;
Iterator<Entry> iterator = iterator();
while (iterator.hasNext()) {
final Entry entry = iterator.next();
if (entry.getValue() >= maxValue) {
maxIndex = entry.getIndex();
maxValue = entry.getValue();
}
}
return maxIndex;
}
/**
* Get the value of the maximum entry.
*
* @return the value of the maximum entry or {@code NaN} if all
* entries are {@code NaN}.
*/
public double getMaxValue() {
final int maxIndex = getMaxIndex();
return maxIndex < 0 ? Double.NaN : getEntry(maxIndex);
}
/**
* Multiply each entry by the argument. Returns a new vector.
* Does not change instance data.
*
* @param d Multiplication factor.
* @return {@code this} * {@code d}.
*/
public RealVector mapMultiply
Math, 29
<FILEB>
<CHANGES>
final int n = getDimension();
for (int i = 0; i < n; i++) {
res.setEntry(i, this.getEntry(i) / v.getEntry(i));
<CHANGEE>
<CHANGES>
if (v.isNaN() || v.isInfinite()) {
final int n = getDimension();
for (int i = 0; i < n; i++) {
final double y = v.getEntry(i);
if (Double.isNaN(y)) {
res.setEntry(i, Double.NaN);
} else if (Double.isInfinite(y)) {
final double x = this.getEntry(i);
res.setEntry(i, x * y);
}
}
}
<CHANGEE>
<FILEE>
<FILEB>
@Override
public double dotProduct(RealVector v) {
if(v instanceof OpenMapRealVector) {
return dotProduct((OpenMapRealVector)v);
} else {
return super.dotProduct(v);
}
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeDivide(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
/*
* MATH-803: it is not sufficient to loop through non zero entries of
* this only. Indeed, if this[i] = 0d and v[i] = 0d, then
* this[i] / v[i] = NaN, and not 0d.
*/
<CHANGES>
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() / v.getEntry(iter.key()));
<CHANGEE>
}
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeMultiply(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() * v.getEntry(iter.key()));
}
/*
* MATH-803: the above loop assumes that 0d * x = 0d for any double x,
* which allows to consider only the non-zero entries of this. However,
* this fails if this[i] == 0d and (v[i] = NaN or v[i] = Infinity).
*
* These special cases are handled below.
*/
<CHANGES>
<CHANGEE>
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector getSubVector(int index, int n) {
checkIndex(index);
if (n < 0) {
throw new NotPositiveException(LocalizedFormats.NUMBER_OF_ELEMENTS_SHOULD_BE_POSITIVE, n);
}
checkIndex(index + n - 1);
OpenMapRealVector<SCANS> }
Iterator<Entry> thisIt = sparseIterator();
while (thisIt.hasNext()) {
final Entry thisE = thisIt.next();
Iterator<Entry> otherIt = v.sparseIterator();
while (otherIt.hasNext()) {
final Entry otherE = otherIt.next();
product.setEntry(thisE.getIndex(), otherE.getIndex(),
thisE.getValue() * otherE.getValue());
}
}
return product;
}
/**
* Find the orthogonal projection of this vector onto another vector.
*
* @param v vector onto which instance must be projected.
* @return projection of the instance onto {@code v}.
* @throws org.apache.commons.math3.exception.DimensionMismatchException
* if {@code v} is not the same size as this vector.
*/
public abstract RealVector projection(RealVector v);
/**
* Set all elements to a single value.
*
* @param value Single value to set for all elements.
*/
public void set(double value) {
Iterator<Entry> it = iterator();
while (it.hasNext()) {
final Entry e = it.next();
e.setValue(value);
}
}
/**
* Convert the vector to an array of {@code double}s.
* The array is independent from this vector data: the elements
* are copied.
*
* @return an array containing a copy of the vector elements.
*/
public double[] toArray() {
int dim = getDimension();
double[] values = new double[dim];
for (int i = 0; i < dim; i++) {
values[i] = getEntry(i);
}
return values;
}
/**
* Creates a unit vector pointing in the direction of this vector.
* The instance is not changed by this method.
*
* @return a unit vector pointing in direction of this vector.
* @throws ArithmeticException if the norm is {@code null}.
*/
public RealVector unitVector() {
RealVector copy = copy();
copy.unitize();
return copy;
}
/**
* Converts this vector into a unit vector.
* The instance itself is changed by this method.
*
* @throws org.apache.commons.math3.exception.Math
Math, 29
<FILEB>
<CHANGES>
final int n = getDimension();
for (int i = 0; i < n; i++) {
res.setEntry(i, this.getEntry(i) / v.getEntry(i));
<CHANGEE>
<CHANGES>
if (v.isNaN() || v.isInfinite()) {
final int n = getDimension();
for (int i = 0; i < n; i++) {
final double y = v.getEntry(i);
if (Double.isNaN(y)) {
res.setEntry(i, Double.NaN);
} else if (Double.isInfinite(y)) {
final double x = this.getEntry(i);
res.setEntry(i, x * y);
}
}
}
<CHANGEE>
<FILEE>
<FILEB>
@Override
public double dotProduct(RealVector v) {
if(v instanceof OpenMapRealVector) {
return dotProduct((OpenMapRealVector)v);
} else {
return super.dotProduct(v);
}
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeDivide(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
/*
* MATH-803: it is not sufficient to loop through non zero entries of
* this only. Indeed, if this[i] = 0d and v[i] = 0d, then
* this[i] / v[i] = NaN, and not 0d.
*/
<CHANGES>
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() / v.getEntry(iter.key()));
<CHANGEE>
}
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeMultiply(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() * v.getEntry(iter.key()));
}
/*
* MATH-803: the above loop assumes that 0d * x = 0d for any double x,
* which allows to consider only the non-zero entries of this. However,
* this fails if this[i] == 0d and (v[i] = NaN or v[i] = Infinity).
*
* These special cases are handled below.
*/
<CHANGES>
<CHANGEE>
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector getSubVector(int index, int n) {
checkIndex(index);
if (n < 0) {
throw new NotPositiveException(LocalizedFormats.NUMBER_OF_ELEMENTS_SHOULD_BE_POSITIVE, n);
}
checkIndex(index + n - 1);
OpenMapRealVector<SCANS>ArithmeticException
* if the norm is zero.
*/
public void unitize() {
mapDivideToSelf(getNorm());
}
/**
* Create a sparse iterator over the vector, which may omit some entries.
* Specialized implementations may choose to not iterate over all
* dimensions, either because those values are unset, or are equal
* to defaultValue(), or are small enough to be ignored for the
* purposes of iteration. No guarantees are made about order of iteration.
* In dense implementations, this method will often delegate to
* {@link #iterator()}.
*
* <p>Note: derived classes are required to return an {@link Iterator} that
* returns non-null {@link Entry} objects as long as {@link Iterator#hasNext()}
* returns {@code true}.</p>
*
* @return a sparse iterator.
*/
public Iterator<Entry> sparseIterator() {
return new SparseEntryIterator();
}
/**
* Generic dense iterator. Iteration is in increasing order
* of the vector index.
*
* <p>Note: derived classes are required to return an {@link Iterator} that
* returns non-null {@link Entry} objects as long as {@link Iterator#hasNext()}
* returns {@code true}.</p>
*
* @return a dense iterator.
*/
public Iterator<Entry> iterator() {
final int dim = getDimension();
return new Iterator<Entry>() {
/** Current index. */
private int i = 0;
/** Current entry. */
private Entry e = new Entry();
/** {@inheritDoc} */
public boolean hasNext() {
return i < dim;
}
/** {@inheritDoc} */
public Entry next() {
e.setIndex(i++);
return e;
}
/** {@inheritDoc} */
public void remove() {
throw new MathUnsupportedOperationException();
}
};
}
/**
* Acts as if implemented as:
* <pre>
* return copy().mapToSelf(function);
* </pre>
* Returns a new vector. Does not change instance data.
*
* @param function Function to apply to each entry.
* @return a new vector.
*/
public RealVector map(UnivariateFunction function) {
return copy().mapToSelf(function);
}
/**
Math, 29
<FILEB>
<CHANGES>
final int n = getDimension();
for (int i = 0; i < n; i++) {
res.setEntry(i, this.getEntry(i) / v.getEntry(i));
<CHANGEE>
<CHANGES>
if (v.isNaN() || v.isInfinite()) {
final int n = getDimension();
for (int i = 0; i < n; i++) {
final double y = v.getEntry(i);
if (Double.isNaN(y)) {
res.setEntry(i, Double.NaN);
} else if (Double.isInfinite(y)) {
final double x = this.getEntry(i);
res.setEntry(i, x * y);
}
}
}
<CHANGEE>
<FILEE>
<FILEB>
@Override
public double dotProduct(RealVector v) {
if(v instanceof OpenMapRealVector) {
return dotProduct((OpenMapRealVector)v);
} else {
return super.dotProduct(v);
}
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeDivide(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
/*
* MATH-803: it is not sufficient to loop through non zero entries of
* this only. Indeed, if this[i] = 0d and v[i] = 0d, then
* this[i] / v[i] = NaN, and not 0d.
*/
<CHANGES>
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() / v.getEntry(iter.key()));
<CHANGEE>
}
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeMultiply(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() * v.getEntry(iter.key()));
}
/*
* MATH-803: the above loop assumes that 0d * x = 0d for any double x,
* which allows to consider only the non-zero entries of this. However,
* this fails if this[i] == 0d and (v[i] = NaN or v[i] = Infinity).
*
* These special cases are handled below.
*/
<CHANGES>
<CHANGEE>
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector getSubVector(int index, int n) {
checkIndex(index);
if (n < 0) {
throw new NotPositiveException(LocalizedFormats.NUMBER_OF_ELEMENTS_SHOULD_BE_POSITIVE, n);
}
checkIndex(index + n - 1);
OpenMapRealVector<SCANS> * Acts as if it is implemented as:
* <pre>
* Entry e = null;
* for(Iterator<Entry> it = iterator(); it.hasNext(); e = it.next()) {
* e.setValue(function.value(e.getValue()));
* }
* </pre>
* Entries of this vector are modified in-place by this method.
*
* @param function Function to apply to each entry.
* @return a reference to this vector.
*/
public RealVector mapToSelf(UnivariateFunction function) {
Iterator<Entry> it = (function.value(0) == 0) ? sparseIterator() : iterator();
while (it.hasNext()) {
final Entry e = it.next();
e.setValue(function.value(e.getValue()));
}
return this;
}
/**
* Returns a new vector representing {@code a * this + b * y}, the linear
* combination of {@code this} and {@code y}.
* Returns a new vector. Does not change instance data.
*
* @param a Coefficient of {@code this}.
* @param b Coefficient of {@code y}.
* @param y Vector with which {@code this} is linearly combined.
* @return a vector containing {@code a * this[i] + b * y[i]} for all
* {@code i}.
* @throws org.apache.commons.math3.exception.DimensionMismatchException
* if {@code y} is not the same size as this vector.
*/
public RealVector combine(double a, double b, RealVector y) {
return copy().combineToSelf(a, b, y);
}
/**
* Updates {@code this} with the linear combination of {@code this} and
* {@code y}.
*
* @param a Weight of {@code this}.
* @param b Weight of {@code y}.
* @param y Vector with which {@code this} is linearly combined.
* @return {@code this}, with components equal to
* {@code a * this[i] + b * y[i]} for all {@code i}.
* @throws org.apache.commons.math3.exception.DimensionMismatchException
* if {@code y} is not the same
Math, 29
<FILEB>
<CHANGES>
final int n = getDimension();
for (int i = 0; i < n; i++) {
res.setEntry(i, this.getEntry(i) / v.getEntry(i));
<CHANGEE>
<CHANGES>
if (v.isNaN() || v.isInfinite()) {
final int n = getDimension();
for (int i = 0; i < n; i++) {
final double y = v.getEntry(i);
if (Double.isNaN(y)) {
res.setEntry(i, Double.NaN);
} else if (Double.isInfinite(y)) {
final double x = this.getEntry(i);
res.setEntry(i, x * y);
}
}
}
<CHANGEE>
<FILEE>
<FILEB>
@Override
public double dotProduct(RealVector v) {
if(v instanceof OpenMapRealVector) {
return dotProduct((OpenMapRealVector)v);
} else {
return super.dotProduct(v);
}
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeDivide(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
/*
* MATH-803: it is not sufficient to loop through non zero entries of
* this only. Indeed, if this[i] = 0d and v[i] = 0d, then
* this[i] / v[i] = NaN, and not 0d.
*/
<CHANGES>
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() / v.getEntry(iter.key()));
<CHANGEE>
}
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeMultiply(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() * v.getEntry(iter.key()));
}
/*
* MATH-803: the above loop assumes that 0d * x = 0d for any double x,
* which allows to consider only the non-zero entries of this. However,
* this fails if this[i] == 0d and (v[i] = NaN or v[i] = Infinity).
*
* These special cases are handled below.
*/
<CHANGES>
<CHANGEE>
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector getSubVector(int index, int n) {
checkIndex(index);
if (n < 0) {
throw new NotPositiveException(LocalizedFormats.NUMBER_OF_ELEMENTS_SHOULD_BE_POSITIVE, n);
}
checkIndex(index + n - 1);
OpenMapRealVector<SCANS> size as this vector.
*/
public RealVector combineToSelf(double a, double b, RealVector y) {
checkVectorDimensions(y);
for (int i = 0; i < getDimension(); i++) {
final double xi = getEntry(i);
final double yi = y.getEntry(i);
setEntry(i, a * xi + b * yi);
}
return this;
}
/**
* Visits (but does not alter) all entries of this vector in default order
* (increasing index).
*
* @param visitor the visitor to be used to process the entries of this
* vector
* @return the value returned by {@link RealVectorPreservingVisitor#end()}
* at the end of the walk
*/
public double walkInDefaultOrder(final RealVectorPreservingVisitor visitor) {
final int dim = getDimension();
visitor.start(dim, 0, dim - 1);
for (int i = 0; i < dim; i++) {
visitor.visit(i, getEntry(i));
}
return visitor.end();
}
/**
* Visits (but does not alter) some entries of this vector in default order
* (increasing index).
*
* @param visitor visitor to be used to process the entries of this vector
* @param start the index of the first entry to be visited
* @param end the index of the last entry to be visited (inclusive)
* @return the value returned by {@link RealVectorPreservingVisitor#end()}
* at the end of the walk
* @throws org.apache.commons.math3.exception.OutOfRangeException if
* the indices are not valid.
*/
public double walkInDefaultOrder(final RealVectorPreservingVisitor visitor,
final int start, final int end) {
checkIndices(start, end);
visitor.start(getDimension(), start, end);
for (int i = start; i <= end; i++) {
visitor.visit(i, getEntry(i));
}
return visitor.end();
}
/**
* Visits (but does not alter) all entries of this vector in optimized
* order. The order in which the entries are visited is selected so as to
* lead to the most efficient implementation; it might depend
Math, 29
<FILEB>
<CHANGES>
final int n = getDimension();
for (int i = 0; i < n; i++) {
res.setEntry(i, this.getEntry(i) / v.getEntry(i));
<CHANGEE>
<CHANGES>
if (v.isNaN() || v.isInfinite()) {
final int n = getDimension();
for (int i = 0; i < n; i++) {
final double y = v.getEntry(i);
if (Double.isNaN(y)) {
res.setEntry(i, Double.NaN);
} else if (Double.isInfinite(y)) {
final double x = this.getEntry(i);
res.setEntry(i, x * y);
}
}
}
<CHANGEE>
<FILEE>
<FILEB>
@Override
public double dotProduct(RealVector v) {
if(v instanceof OpenMapRealVector) {
return dotProduct((OpenMapRealVector)v);
} else {
return super.dotProduct(v);
}
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeDivide(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
/*
* MATH-803: it is not sufficient to loop through non zero entries of
* this only. Indeed, if this[i] = 0d and v[i] = 0d, then
* this[i] / v[i] = NaN, and not 0d.
*/
<CHANGES>
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() / v.getEntry(iter.key()));
<CHANGEE>
}
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeMultiply(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() * v.getEntry(iter.key()));
}
/*
* MATH-803: the above loop assumes that 0d * x = 0d for any double x,
* which allows to consider only the non-zero entries of this. However,
* this fails if this[i] == 0d and (v[i] = NaN or v[i] = Infinity).
*
* These special cases are handled below.
*/
<CHANGES>
<CHANGEE>
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector getSubVector(int index, int n) {
checkIndex(index);
if (n < 0) {
throw new NotPositiveException(LocalizedFormats.NUMBER_OF_ELEMENTS_SHOULD_BE_POSITIVE, n);
}
checkIndex(index + n - 1);
OpenMapRealVector<SCANS> on the
* concrete implementation of this abstract class.
*
* @param visitor the visitor to be used to process the entries of this
* vector
* @return the value returned by {@link RealVectorPreservingVisitor#end()}
* at the end of the walk
*/
public double walkInOptimizedOrder(final RealVectorPreservingVisitor visitor) {
return walkInDefaultOrder(visitor);
}
/**
* Visits (but does not alter) some entries of this vector in optimized
* order. The order in which the entries are visited is selected so as to
* lead to the most efficient implementation; it might depend on the
* concrete implementation of this abstract class.
*
* @param visitor visitor to be used to process the entries of this vector
* @param start the index of the first entry to be visited
* @param end the index of the last entry to be visited (inclusive)
* @return the value returned by {@link RealVectorPreservingVisitor#end()}
* at the end of the walk
* @throws org.apache.commons.math3.exception.OutOfRangeException if
* the indices are not valid.
*/
public double walkInOptimizedOrder(final RealVectorPreservingVisitor visitor,
final int start, final int end) {
return walkInDefaultOrder(visitor, start, end);
}
/**
* Visits (and possibly alters) all entries of this vector in default order
* (increasing index).
*
* @param visitor the visitor to be used to process and modify the entries
* of this vector
* @return the value returned by {@link RealVectorChangingVisitor#end()}
* at the end of the walk
*/
public double walkInDefaultOrder(final RealVectorChangingVisitor visitor) {
final int dim = getDimension();
visitor.start(dim, 0, dim - 1);
for (int i = 0; i < dim; i++) {
setEntry(i, visitor.visit(i, getEntry(i)));
}
return visitor.end();
}
/**
* Visits (and possibly alters) some entries of this vector in default order
* (increasing index).
*
* @param visitor visitor to be used to process the entries of this vector
* @param start the index of the first entry to be
Math, 29
<FILEB>
<CHANGES>
final int n = getDimension();
for (int i = 0; i < n; i++) {
res.setEntry(i, this.getEntry(i) / v.getEntry(i));
<CHANGEE>
<CHANGES>
if (v.isNaN() || v.isInfinite()) {
final int n = getDimension();
for (int i = 0; i < n; i++) {
final double y = v.getEntry(i);
if (Double.isNaN(y)) {
res.setEntry(i, Double.NaN);
} else if (Double.isInfinite(y)) {
final double x = this.getEntry(i);
res.setEntry(i, x * y);
}
}
}
<CHANGEE>
<FILEE>
<FILEB>
@Override
public double dotProduct(RealVector v) {
if(v instanceof OpenMapRealVector) {
return dotProduct((OpenMapRealVector)v);
} else {
return super.dotProduct(v);
}
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeDivide(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
/*
* MATH-803: it is not sufficient to loop through non zero entries of
* this only. Indeed, if this[i] = 0d and v[i] = 0d, then
* this[i] / v[i] = NaN, and not 0d.
*/
<CHANGES>
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() / v.getEntry(iter.key()));
<CHANGEE>
}
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeMultiply(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() * v.getEntry(iter.key()));
}
/*
* MATH-803: the above loop assumes that 0d * x = 0d for any double x,
* which allows to consider only the non-zero entries of this. However,
* this fails if this[i] == 0d and (v[i] = NaN or v[i] = Infinity).
*
* These special cases are handled below.
*/
<CHANGES>
<CHANGEE>
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector getSubVector(int index, int n) {
checkIndex(index);
if (n < 0) {
throw new NotPositiveException(LocalizedFormats.NUMBER_OF_ELEMENTS_SHOULD_BE_POSITIVE, n);
}
checkIndex(index + n - 1);
OpenMapRealVector<SCANS> visited
* @param end the index of the last entry to be visited (inclusive)
* @return the value returned by {@link RealVectorChangingVisitor#end()}
* at the end of the walk
* @throws org.apache.commons.math3.exception.OutOfRangeException if
* the indices are not valid.
*/
public double walkInDefaultOrder(final RealVectorChangingVisitor visitor,
final int start, final int end) {
checkIndices(start, end);
visitor.start(getDimension(), start, end);
for (int i = start; i <= end; i++) {
setEntry(i, visitor.visit(i, getEntry(i)));
}
return visitor.end();
}
/**
* Visits (and possibly alters) all entries of this vector in optimized
* order. The order in which the entries are visited is selected so as to
* lead to the most efficient implementation; it might depend on the
* concrete implementation of this abstract class.
*
* @param visitor the visitor to be used to process the entries of this
* vector
* @return the value returned by {@link RealVectorChangingVisitor#end()}
* at the end of the walk
*/
public double walkInOptimizedOrder(final RealVectorChangingVisitor visitor) {
return walkInDefaultOrder(visitor);
}
/**
* Visits (and possibly change) some entries of this vector in optimized
* order. The order in which the entries are visited is selected so as to
* lead to the most efficient implementation; it might depend on the
* concrete implementation of this abstract class.
*
* @param visitor visitor to be used to process the entries of this vector
* @param start the index of the first entry to be visited
* @param end the index of the last entry to be visited (inclusive)
* @return the value returned by {@link RealVectorChangingVisitor#end()}
* at the end of the walk
* @throws org.apache.commons.math3.exception.OutOfRangeException if
* the indices are not valid.
*/
public double walkInOptimizedOrder(final RealVectorChangingVisitor visitor,
final int start, final int end) {
return walkInDefaultOrder(visitor, start, end);
}
/** An entry in the vector. */
protected class Entry {
/** Index
Math, 29
<FILEB>
<CHANGES>
final int n = getDimension();
for (int i = 0; i < n; i++) {
res.setEntry(i, this.getEntry(i) / v.getEntry(i));
<CHANGEE>
<CHANGES>
if (v.isNaN() || v.isInfinite()) {
final int n = getDimension();
for (int i = 0; i < n; i++) {
final double y = v.getEntry(i);
if (Double.isNaN(y)) {
res.setEntry(i, Double.NaN);
} else if (Double.isInfinite(y)) {
final double x = this.getEntry(i);
res.setEntry(i, x * y);
}
}
}
<CHANGEE>
<FILEE>
<FILEB>
@Override
public double dotProduct(RealVector v) {
if(v instanceof OpenMapRealVector) {
return dotProduct((OpenMapRealVector)v);
} else {
return super.dotProduct(v);
}
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeDivide(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
/*
* MATH-803: it is not sufficient to loop through non zero entries of
* this only. Indeed, if this[i] = 0d and v[i] = 0d, then
* this[i] / v[i] = NaN, and not 0d.
*/
<CHANGES>
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() / v.getEntry(iter.key()));
<CHANGEE>
}
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeMultiply(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() * v.getEntry(iter.key()));
}
/*
* MATH-803: the above loop assumes that 0d * x = 0d for any double x,
* which allows to consider only the non-zero entries of this. However,
* this fails if this[i] == 0d and (v[i] = NaN or v[i] = Infinity).
*
* These special cases are handled below.
*/
<CHANGES>
<CHANGEE>
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector getSubVector(int index, int n) {
checkIndex(index);
if (n < 0) {
throw new NotPositiveException(LocalizedFormats.NUMBER_OF_ELEMENTS_SHOULD_BE_POSITIVE, n);
}
checkIndex(index + n - 1);
OpenMapRealVector<SCANS>Vector mapToSelf(UnivariateFunction function) {
throw new MathUnsupportedOperationException();
}
/** {@inheritDoc} */
@Override
public RealVector map(UnivariateFunction function) {
return v.map(function);
}
/** {@inheritDoc} */
@Override
public Iterator<Entry> iterator() {
final Iterator<Entry> i = v.iterator();
return new Iterator<Entry>() {
/** The current entry. */
private final UnmodifiableEntry e = new UnmodifiableEntry();
/** {@inheritDoc} */
public boolean hasNext() {
return i.hasNext();
}
/** {@inheritDoc} */
public Entry next() {
e.setIndex(i.next().getIndex());
return e;
}
/** {@inheritDoc} */
public void remove() {
throw new MathUnsupportedOperationException();
}
};
}
/** {@inheritDoc} */
@Override
public Iterator<Entry> sparseIterator() {
final Iterator<Entry> i = v.sparseIterator();
return new Iterator<Entry>() {
/** The current entry. */
private final UnmodifiableEntry e = new UnmodifiableEntry();
/** {@inheritDoc} */
public boolean hasNext() {
return i.hasNext();
}
/** {@inheritDoc} */
public Entry next() {
e.setIndex(i.next().getIndex());
return e;
}
/** {@inheritDoc} */
public void remove() {
throw new MathUnsupportedOperationException();
}
};
}
/** {@inheritDoc} */
@Override
public RealVector copy() {
return v.copy();
}
/** {@inheritDoc} */
@Override
public RealVector add(RealVector w) {
return v.add(w);
}
/** {@inheritDoc} */
@Override
public RealVector subtract(RealVector w) {
return v.subtract(w);
}
/** {@inheritDoc} */
@Override
public RealVector mapAdd(double d) {
return v.mapAdd(d);
}
/** {@inheritDoc} */
@Override
public RealVector mapAddToSelf(double d) {
throw new MathUnsupportedOperationException();
}
/** {@inheritDoc} */
@Override
public RealVector mapSubtract(double d) {
return v.mapSubtract(d);
}
/** {@inheritDoc} */
@Override
public Real
Math, 29
<FILEB>
<CHANGES>
final int n = getDimension();
for (int i = 0; i < n; i++) {
res.setEntry(i, this.getEntry(i) / v.getEntry(i));
<CHANGEE>
<CHANGES>
if (v.isNaN() || v.isInfinite()) {
final int n = getDimension();
for (int i = 0; i < n; i++) {
final double y = v.getEntry(i);
if (Double.isNaN(y)) {
res.setEntry(i, Double.NaN);
} else if (Double.isInfinite(y)) {
final double x = this.getEntry(i);
res.setEntry(i, x * y);
}
}
}
<CHANGEE>
<FILEE>
<FILEB>
@Override
public double dotProduct(RealVector v) {
if(v instanceof OpenMapRealVector) {
return dotProduct((OpenMapRealVector)v);
} else {
return super.dotProduct(v);
}
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeDivide(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
/*
* MATH-803: it is not sufficient to loop through non zero entries of
* this only. Indeed, if this[i] = 0d and v[i] = 0d, then
* this[i] / v[i] = NaN, and not 0d.
*/
<CHANGES>
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() / v.getEntry(iter.key()));
<CHANGEE>
}
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeMultiply(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() * v.getEntry(iter.key()));
}
/*
* MATH-803: the above loop assumes that 0d * x = 0d for any double x,
* which allows to consider only the non-zero entries of this. However,
* this fails if this[i] == 0d and (v[i] = NaN or v[i] = Infinity).
*
* These special cases are handled below.
*/
<CHANGES>
<CHANGEE>
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector getSubVector(int index, int n) {
checkIndex(index);
if (n < 0) {
throw new NotPositiveException(LocalizedFormats.NUMBER_OF_ELEMENTS_SHOULD_BE_POSITIVE, n);
}
checkIndex(index + n - 1);
OpenMapRealVector<SCANS>inheritDoc} */
@Override
public RealVector projection(RealVector w) {
return v.projection(w);
}
/** {@inheritDoc} */
@Override
public RealMatrix outerProduct(RealVector w) {
return v.outerProduct(w);
}
/** {@inheritDoc} */
@Override
public double getEntry(int index) {
return v.getEntry(index);
}
/** {@inheritDoc} */
@Override
public void setEntry(int index, double value) {
throw new MathUnsupportedOperationException();
}
/** {@inheritDoc} */
@Override
public void addToEntry(int index, double value) {
throw new MathUnsupportedOperationException();
}
/** {@inheritDoc} */
@Override
public int getDimension() {
return v.getDimension();
}
/** {@inheritDoc} */
@Override
public RealVector append(RealVector w) {
return v.append(w);
}
/** {@inheritDoc} */
@Override
public RealVector append(double d) {
return v.append(d);
}
/** {@inheritDoc} */
@Override
public RealVector getSubVector(int index, int n) {
return v.getSubVector(index, n);
}
/** {@inheritDoc} */
@Override
public void setSubVector(int index, RealVector w) {
throw new MathUnsupportedOperationException();
}
/** {@inheritDoc} */
@Override
public void set(double value) {
throw new MathUnsupportedOperationException();
}
/** {@inheritDoc} */
@Override
public double[] toArray() {
return v.toArray();
}
/** {@inheritDoc} */
@Override
public boolean isNaN() {
return v.isNaN();
}
/** {@inheritDoc} */
@Override
public boolean isInfinite() {
return v.isInfinite();
}
/** {@inheritDoc} */
@Override
public RealVector combine(double a, double b, RealVector y) {
return v.combine(a, b, y);
}
/** {@inheritDoc} */
@Override
public RealVector combineToSelf(double a, double b, RealVector y) {
throw new MathUnsupportedOperationException();
}
/** An entry in the vector. */
class UnmodifiableEntry extends Entry {
/** {@inheritDoc} */
@Override
Math, 29
<FILEB>
<CHANGES>
final int n = getDimension();
for (int i = 0; i < n; i++) {
res.setEntry(i, this.getEntry(i) / v.getEntry(i));
<CHANGEE>
<CHANGES>
if (v.isNaN() || v.isInfinite()) {
final int n = getDimension();
for (int i = 0; i < n; i++) {
final double y = v.getEntry(i);
if (Double.isNaN(y)) {
res.setEntry(i, Double.NaN);
} else if (Double.isInfinite(y)) {
final double x = this.getEntry(i);
res.setEntry(i, x * y);
}
}
}
<CHANGEE>
<FILEE>
<FILEB>
@Override
public double dotProduct(RealVector v) {
if(v instanceof OpenMapRealVector) {
return dotProduct((OpenMapRealVector)v);
} else {
return super.dotProduct(v);
}
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeDivide(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
/*
* MATH-803: it is not sufficient to loop through non zero entries of
* this only. Indeed, if this[i] = 0d and v[i] = 0d, then
* this[i] / v[i] = NaN, and not 0d.
*/
<CHANGES>
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() / v.getEntry(iter.key()));
<CHANGEE>
}
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeMultiply(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() * v.getEntry(iter.key()));
}
/*
* MATH-803: the above loop assumes that 0d * x = 0d for any double x,
* which allows to consider only the non-zero entries of this. However,
* this fails if this[i] == 0d and (v[i] = NaN or v[i] = Infinity).
*
* These special cases are handled below.
*/
<CHANGES>
<CHANGEE>
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector getSubVector(int index, int n) {
checkIndex(index);
if (n < 0) {
throw new NotPositiveException(LocalizedFormats.NUMBER_OF_ELEMENTS_SHOULD_BE_POSITIVE, n);
}
checkIndex(index + n - 1);
OpenMapRealVector<SCANS> static final double F_13_14 = 13d / 14d;
/** Constant: {@value}. */
private static final double F_11_12 = 11d / 12d;
/** Constant: {@value}. */
private static final double F_9_10 = 9d / 10d;
/** Constant: {@value}. */
private static final double F_7_8 = 7d / 8d;
/** Constant: {@value}. */
private static final double F_5_6 = 5d / 6d;
/** Constant: {@value}. */
private static final double F_1_2 = 1d / 2d;
/** Constant: {@value}. */
private static final double F_1_4 = 1d / 4d;
/**
* Private Constructor
*/
private FastMath() {}
// Generic helper methods
/**
* Get the high order bits from the mantissa.
* Equivalent to adding and subtracting HEX_40000 but also works for very large numbers
*
* @param d the value to split
* @return the high order part of the mantissa
*/
private static double doubleHighPart(double d) {
if (d > -Precision.SAFE_MIN && d < Precision.SAFE_MIN){
return d; // These are un-normalised - don't try to convert
}
long xl = Double.doubleToLongBits(d);
xl = xl & MASK_30BITS; // Drop low order bits
return Double.longBitsToDouble(xl);
}
/** Compute the square root of a number.
* <p><b>Note:</b> this implementation currently delegates to {@link Math#sqrt}
* @param a number on which evaluation is done
* @return square root of a
*/
public static double sqrt(final double a) {
return Math.sqrt(a);
}
/** Compute the hyperbolic cosine of a number.
* @param x number on which evaluation is done
* @return hyperbolic cosine of x
*/
public static double cosh(double x) {
if (x != x) {
return x;
}
// cosh[z] =
Math, 29
<FILEB>
<CHANGES>
final int n = getDimension();
for (int i = 0; i < n; i++) {
res.setEntry(i, this.getEntry(i) / v.getEntry(i));
<CHANGEE>
<CHANGES>
if (v.isNaN() || v.isInfinite()) {
final int n = getDimension();
for (int i = 0; i < n; i++) {
final double y = v.getEntry(i);
if (Double.isNaN(y)) {
res.setEntry(i, Double.NaN);
} else if (Double.isInfinite(y)) {
final double x = this.getEntry(i);
res.setEntry(i, x * y);
}
}
}
<CHANGEE>
<FILEE>
<FILEB>
@Override
public double dotProduct(RealVector v) {
if(v instanceof OpenMapRealVector) {
return dotProduct((OpenMapRealVector)v);
} else {
return super.dotProduct(v);
}
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeDivide(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
/*
* MATH-803: it is not sufficient to loop through non zero entries of
* this only. Indeed, if this[i] = 0d and v[i] = 0d, then
* this[i] / v[i] = NaN, and not 0d.
*/
<CHANGES>
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() / v.getEntry(iter.key()));
<CHANGEE>
}
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeMultiply(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() * v.getEntry(iter.key()));
}
/*
* MATH-803: the above loop assumes that 0d * x = 0d for any double x,
* which allows to consider only the non-zero entries of this. However,
* this fails if this[i] == 0d and (v[i] = NaN or v[i] = Infinity).
*
* These special cases are handled below.
*/
<CHANGES>
<CHANGEE>
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector getSubVector(int index, int n) {
checkIndex(index);
if (n < 0) {
throw new NotPositiveException(LocalizedFormats.NUMBER_OF_ELEMENTS_SHOULD_BE_POSITIVE, n);
}
checkIndex(index + n - 1);
OpenMapRealVector<SCANS> (exp(z) + exp(-z))/2
// for numbers with magnitude 20 or so,
// exp(-z) can be ignored in comparison with exp(z)
if (x > 20.0) {
return exp(x)/2.0;
}
if (x < -20) {
return exp(-x)/2.0;
}
double hiPrec[] = new double[2];
if (x < 0.0) {
x = -x;
}
exp(x, 0.0, hiPrec);
double ya = hiPrec[0] + hiPrec[1];
double yb = -(ya - hiPrec[0] - hiPrec[1]);
double temp = ya * HEX_40000000;
double yaa = ya + temp - temp;
double yab = ya - yaa;
// recip = 1/y
double recip = 1.0/ya;
temp = recip * HEX_40000000;
double recipa = recip + temp - temp;
double recipb = recip - recipa;
// Correct for rounding in division
recipb += (1.0 - yaa*recipa - yaa*recipb - yab*recipa - yab*recipb) * recip;
// Account for yb
recipb += -yb * recip * recip;
// y = y + 1/y
temp = ya + recipa;
yb += -(temp - ya - recipa);
ya = temp;
temp = ya + recipb;
yb += -(temp - ya - recipb);
ya = temp;
double result = ya + yb;
result *= 0.5;
return result;
}
/** Compute the hyperbolic sine of a number.
* @param x number on which evaluation is done
* @return hyperbolic sine of x
*/
public static double sinh(double x) {
boolean negate = false;
if (x != x) {
return x;
}
// sinh[z] = (exp(z) - exp(-z) / 2
// for
Math, 29
<FILEB>
<CHANGES>
final int n = getDimension();
for (int i = 0; i < n; i++) {
res.setEntry(i, this.getEntry(i) / v.getEntry(i));
<CHANGEE>
<CHANGES>
if (v.isNaN() || v.isInfinite()) {
final int n = getDimension();
for (int i = 0; i < n; i++) {
final double y = v.getEntry(i);
if (Double.isNaN(y)) {
res.setEntry(i, Double.NaN);
} else if (Double.isInfinite(y)) {
final double x = this.getEntry(i);
res.setEntry(i, x * y);
}
}
}
<CHANGEE>
<FILEE>
<FILEB>
@Override
public double dotProduct(RealVector v) {
if(v instanceof OpenMapRealVector) {
return dotProduct((OpenMapRealVector)v);
} else {
return super.dotProduct(v);
}
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeDivide(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
/*
* MATH-803: it is not sufficient to loop through non zero entries of
* this only. Indeed, if this[i] = 0d and v[i] = 0d, then
* this[i] / v[i] = NaN, and not 0d.
*/
<CHANGES>
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() / v.getEntry(iter.key()));
<CHANGEE>
}
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeMultiply(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() * v.getEntry(iter.key()));
}
/*
* MATH-803: the above loop assumes that 0d * x = 0d for any double x,
* which allows to consider only the non-zero entries of this. However,
* this fails if this[i] == 0d and (v[i] = NaN or v[i] = Infinity).
*
* These special cases are handled below.
*/
<CHANGES>
<CHANGEE>
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector getSubVector(int index, int n) {
checkIndex(index);
if (n < 0) {
throw new NotPositiveException(LocalizedFormats.NUMBER_OF_ELEMENTS_SHOULD_BE_POSITIVE, n);
}
checkIndex(index + n - 1);
OpenMapRealVector<SCANS> values of z larger than about 20,
// exp(-z) can be ignored in comparison with exp(z)
if (x > 20.0) {
return exp(x)/2.0;
}
if (x < -20) {
return -exp(-x)/2.0;
}
if (x == 0) {
return x;
}
if (x < 0.0) {
x = -x;
negate = true;
}
double result;
if (x > 0.25) {
double hiPrec[] = new double[2];
exp(x, 0.0, hiPrec);
double ya = hiPrec[0] + hiPrec[1];
double yb = -(ya - hiPrec[0] - hiPrec[1]);
double temp = ya * HEX_40000000;
double yaa = ya + temp - temp;
double yab = ya - yaa;
// recip = 1/y
double recip = 1.0/ya;
temp = recip * HEX_40000000;
double recipa = recip + temp - temp;
double recipb = recip - recipa;
// Correct for rounding in division
recipb += (1.0 - yaa*recipa - yaa*recipb - yab*recipa - yab*recipb) * recip;
// Account for yb
recipb += -yb * recip * recip;
recipa = -recipa;
recipb = -recipb;
// y = y + 1/y
temp = ya + recipa;
yb += -(temp - ya - recipa);
ya = temp;
temp = ya + recipb;
yb += -(temp - ya - recipb);
ya = temp;
result = ya + yb;
result *= 0.5;
}
else {
double hiPrec[] = new double[2];
expm1(x, hiPrec);
double ya = hiPrec[0] + hiPrec[1];
double yb = -(ya - hiPrec[0] - hiPrec
Math, 29
<FILEB>
<CHANGES>
final int n = getDimension();
for (int i = 0; i < n; i++) {
res.setEntry(i, this.getEntry(i) / v.getEntry(i));
<CHANGEE>
<CHANGES>
if (v.isNaN() || v.isInfinite()) {
final int n = getDimension();
for (int i = 0; i < n; i++) {
final double y = v.getEntry(i);
if (Double.isNaN(y)) {
res.setEntry(i, Double.NaN);
} else if (Double.isInfinite(y)) {
final double x = this.getEntry(i);
res.setEntry(i, x * y);
}
}
}
<CHANGEE>
<FILEE>
<FILEB>
@Override
public double dotProduct(RealVector v) {
if(v instanceof OpenMapRealVector) {
return dotProduct((OpenMapRealVector)v);
} else {
return super.dotProduct(v);
}
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeDivide(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
/*
* MATH-803: it is not sufficient to loop through non zero entries of
* this only. Indeed, if this[i] = 0d and v[i] = 0d, then
* this[i] / v[i] = NaN, and not 0d.
*/
<CHANGES>
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() / v.getEntry(iter.key()));
<CHANGEE>
}
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeMultiply(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() * v.getEntry(iter.key()));
}
/*
* MATH-803: the above loop assumes that 0d * x = 0d for any double x,
* which allows to consider only the non-zero entries of this. However,
* this fails if this[i] == 0d and (v[i] = NaN or v[i] = Infinity).
*
* These special cases are handled below.
*/
<CHANGES>
<CHANGEE>
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector getSubVector(int index, int n) {
checkIndex(index);
if (n < 0) {
throw new NotPositiveException(LocalizedFormats.NUMBER_OF_ELEMENTS_SHOULD_BE_POSITIVE, n);
}
checkIndex(index + n - 1);
OpenMapRealVector<SCANS>[1]);
/* Compute expm1(-x) = -expm1(x) / (expm1(x) + 1) */
double denom = 1.0 + ya;
double denomr = 1.0 / denom;
double denomb = -(denom - 1.0 - ya) + yb;
double ratio = ya * denomr;
double temp = ratio * HEX_40000000;
double ra = ratio + temp - temp;
double rb = ratio - ra;
temp = denom * HEX_40000000;
double za = denom + temp - temp;
double zb = denom - za;
rb += (ya - za*ra - za*rb - zb*ra - zb*rb) * denomr;
// Adjust for yb
rb += yb*denomr; // numerator
rb += -ya * denomb * denomr * denomr; // denominator
// y = y - 1/y
temp = ya + ra;
yb += -(temp - ya - ra);
ya = temp;
temp = ya + rb;
yb += -(temp - ya - rb);
ya = temp;
result = ya + yb;
result *= 0.5;
}
if (negate) {
result = -result;
}
return result;
}
/** Compute the hyperbolic tangent of a number.
* @param x number on which evaluation is done
* @return hyperbolic tangent of x
*/
public static double tanh(double x) {
boolean negate = false;
if (x != x) {
return x;
}
// tanh[z] = sinh[z] / cosh[z]
// = (exp(z) - exp(-z)) / (exp(z) + exp(-z))
// = (exp(2x) - 1) / (exp(2x) + 1)
// for magnitude > 20, sinh[z] == cosh[z] in double precision
if (x > 20.0) {
return 1.0;
}
if (x < -20) {
return -1.0;
Math, 29
<FILEB>
<CHANGES>
final int n = getDimension();
for (int i = 0; i < n; i++) {
res.setEntry(i, this.getEntry(i) / v.getEntry(i));
<CHANGEE>
<CHANGES>
if (v.isNaN() || v.isInfinite()) {
final int n = getDimension();
for (int i = 0; i < n; i++) {
final double y = v.getEntry(i);
if (Double.isNaN(y)) {
res.setEntry(i, Double.NaN);
} else if (Double.isInfinite(y)) {
final double x = this.getEntry(i);
res.setEntry(i, x * y);
}
}
}
<CHANGEE>
<FILEE>
<FILEB>
@Override
public double dotProduct(RealVector v) {
if(v instanceof OpenMapRealVector) {
return dotProduct((OpenMapRealVector)v);
} else {
return super.dotProduct(v);
}
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeDivide(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
/*
* MATH-803: it is not sufficient to loop through non zero entries of
* this only. Indeed, if this[i] = 0d and v[i] = 0d, then
* this[i] / v[i] = NaN, and not 0d.
*/
<CHANGES>
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() / v.getEntry(iter.key()));
<CHANGEE>
}
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeMultiply(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() * v.getEntry(iter.key()));
}
/*
* MATH-803: the above loop assumes that 0d * x = 0d for any double x,
* which allows to consider only the non-zero entries of this. However,
* this fails if this[i] == 0d and (v[i] = NaN or v[i] = Infinity).
*
* These special cases are handled below.
*/
<CHANGES>
<CHANGEE>
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector getSubVector(int index, int n) {
checkIndex(index);
if (n < 0) {
throw new NotPositiveException(LocalizedFormats.NUMBER_OF_ELEMENTS_SHOULD_BE_POSITIVE, n);
}
checkIndex(index + n - 1);
OpenMapRealVector<SCANS>
}
if (x == 0) {
return x;
}
if (x < 0.0) {
x = -x;
negate = true;
}
double result;
if (x >= 0.5) {
double hiPrec[] = new double[2];
// tanh(x) = (exp(2x) - 1) / (exp(2x) + 1)
exp(x*2.0, 0.0, hiPrec);
double ya = hiPrec[0] + hiPrec[1];
double yb = -(ya - hiPrec[0] - hiPrec[1]);
/* Numerator */
double na = -1.0 + ya;
double nb = -(na + 1.0 - ya);
double temp = na + yb;
nb += -(temp - na - yb);
na = temp;
/* Denominator */
double da = 1.0 + ya;
double db = -(da - 1.0 - ya);
temp = da + yb;
db += -(temp - da - yb);
da = temp;
temp = da * HEX_40000000;
double daa = da + temp - temp;
double dab = da - daa;
// ratio = na/da
double ratio = na/da;
temp = ratio * HEX_40000000;
double ratioa = ratio + temp - temp;
double ratiob = ratio - ratioa;
// Correct for rounding in division
ratiob += (na - daa*ratioa - daa*ratiob - dab*ratioa - dab*ratiob) / da;
// Account for nb
ratiob += nb / da;
// Account for db
ratiob += -db * na / da / da;
result = ratioa + ratiob;
}
else {
double hiPrec[] = new double[2];
// tanh(x) = expm1(2x) / (expm1(2x) + 2)
expm1(x*2.0, hiPrec);
double ya = hiPrec[0] + hiPrec[1];
double yb = -(ya -
Math, 29
<FILEB>
<CHANGES>
final int n = getDimension();
for (int i = 0; i < n; i++) {
res.setEntry(i, this.getEntry(i) / v.getEntry(i));
<CHANGEE>
<CHANGES>
if (v.isNaN() || v.isInfinite()) {
final int n = getDimension();
for (int i = 0; i < n; i++) {
final double y = v.getEntry(i);
if (Double.isNaN(y)) {
res.setEntry(i, Double.NaN);
} else if (Double.isInfinite(y)) {
final double x = this.getEntry(i);
res.setEntry(i, x * y);
}
}
}
<CHANGEE>
<FILEE>
<FILEB>
@Override
public double dotProduct(RealVector v) {
if(v instanceof OpenMapRealVector) {
return dotProduct((OpenMapRealVector)v);
} else {
return super.dotProduct(v);
}
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeDivide(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
/*
* MATH-803: it is not sufficient to loop through non zero entries of
* this only. Indeed, if this[i] = 0d and v[i] = 0d, then
* this[i] / v[i] = NaN, and not 0d.
*/
<CHANGES>
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() / v.getEntry(iter.key()));
<CHANGEE>
}
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeMultiply(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() * v.getEntry(iter.key()));
}
/*
* MATH-803: the above loop assumes that 0d * x = 0d for any double x,
* which allows to consider only the non-zero entries of this. However,
* this fails if this[i] == 0d and (v[i] = NaN or v[i] = Infinity).
*
* These special cases are handled below.
*/
<CHANGES>
<CHANGEE>
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector getSubVector(int index, int n) {
checkIndex(index);
if (n < 0) {
throw new NotPositiveException(LocalizedFormats.NUMBER_OF_ELEMENTS_SHOULD_BE_POSITIVE, n);
}
checkIndex(index + n - 1);
OpenMapRealVector<SCANS>um of a number.
* The signum is -1 for negative numbers, +1 for positive numbers and 0 otherwise
* @param a number on which evaluation is done
* @return -1.0, -0.0, +0.0, +1.0 or NaN depending on sign of a
*/
public static float signum(final float a) {
return (a < 0.0f) ? -1.0f : ((a > 0.0f) ? 1.0f : a); // return +0.0/-0.0/NaN depending on a
}
/** Compute next number towards positive infinity.
* @param a number to which neighbor should be computed
* @return neighbor of a towards positive infinity
*/
public static double nextUp(final double a) {
return nextAfter(a, Double.POSITIVE_INFINITY);
}
/** Compute next number towards positive infinity.
* @param a number to which neighbor should be computed
* @return neighbor of a towards positive infinity
*/
public static float nextUp(final float a) {
return nextAfter(a, Float.POSITIVE_INFINITY);
}
/** Returns a pseudo-random number between 0.0 and 1.0.
* <p><b>Note:</b> this implementation currently delegates to {@link Math#random}
* @return a random number between 0.0 and 1.0
*/
public static double random() {
return Math.random();
}
/**
* Exponential function.
*
* Computes exp(x), function result is nearly rounded. It will be correctly
* rounded to the theoretical value for 99.9% of input values, otherwise it will
* have a 1 UPL error.
*
* Method:
* Lookup intVal = exp(int(x))
* Lookup fracVal = exp(int(x-int(x) / 1024.0) * 1024.0 );
* Compute z as the exponential of the remaining bits by a polynomial minus one
* exp(x) = intVal * fracVal * (1 + z)
*
* Accuracy:
* Calculation is done with 63 bits of precision, so result should be correctly
*
Math, 29
<FILEB>
<CHANGES>
final int n = getDimension();
for (int i = 0; i < n; i++) {
res.setEntry(i, this.getEntry(i) / v.getEntry(i));
<CHANGEE>
<CHANGES>
if (v.isNaN() || v.isInfinite()) {
final int n = getDimension();
for (int i = 0; i < n; i++) {
final double y = v.getEntry(i);
if (Double.isNaN(y)) {
res.setEntry(i, Double.NaN);
} else if (Double.isInfinite(y)) {
final double x = this.getEntry(i);
res.setEntry(i, x * y);
}
}
}
<CHANGEE>
<FILEE>
<FILEB>
@Override
public double dotProduct(RealVector v) {
if(v instanceof OpenMapRealVector) {
return dotProduct((OpenMapRealVector)v);
} else {
return super.dotProduct(v);
}
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeDivide(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
/*
* MATH-803: it is not sufficient to loop through non zero entries of
* this only. Indeed, if this[i] = 0d and v[i] = 0d, then
* this[i] / v[i] = NaN, and not 0d.
*/
<CHANGES>
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() / v.getEntry(iter.key()));
<CHANGEE>
}
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeMultiply(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() * v.getEntry(iter.key()));
}
/*
* MATH-803: the above loop assumes that 0d * x = 0d for any double x,
* which allows to consider only the non-zero entries of this. However,
* this fails if this[i] == 0d and (v[i] = NaN or v[i] = Infinity).
*
* These special cases are handled below.
*/
<CHANGES>
<CHANGEE>
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector getSubVector(int index, int n) {
checkIndex(index);
if (n < 0) {
throw new NotPositiveException(LocalizedFormats.NUMBER_OF_ELEMENTS_SHOULD_BE_POSITIVE, n);
}
checkIndex(index + n - 1);
OpenMapRealVector<SCANS> rounded for 99.9% of input values, with less than 1 ULP error otherwise.
*
* @param x a double
* @return double e<sup>x</sup>
*/
public static double exp(double x) {
return exp(x, 0.0, null);
}
/**
* Internal helper method for exponential function.
* @param x original argument of the exponential function
* @param extra extra bits of precision on input (To Be Confirmed)
* @param hiPrec extra bits of precision on output (To Be Confirmed)
* @return exp(x)
*/
private static double exp(double x, double extra, double[] hiPrec) {
double intPartA;
double intPartB;
int intVal;
/* Lookup exp(floor(x)).
* intPartA will have the upper 22 bits, intPartB will have the lower
* 52 bits.
*/
if (x < 0.0) {
intVal = (int) -x;
if (intVal > 746) {
if (hiPrec != null) {
hiPrec[0] = 0.0;
hiPrec[1] = 0.0;
}
return 0.0;
}
if (intVal > 709) {
/* This will produce a subnormal output */
final double result = exp(x+40.19140625, extra, hiPrec) / 285040095144011776.0;
if (hiPrec != null) {
hiPrec[0] /= 285040095144011776.0;
hiPrec[1] /= 285040095144011776.0;
}
return result;
}
if (intVal == 709) {
/* exp(1.494140625) is nearly a machine number... */
final double result = exp(x+1.494140625, extra, hiPrec) / 4.4555059
Math, 29
<FILEB>
<CHANGES>
final int n = getDimension();
for (int i = 0; i < n; i++) {
res.setEntry(i, this.getEntry(i) / v.getEntry(i));
<CHANGEE>
<CHANGES>
if (v.isNaN() || v.isInfinite()) {
final int n = getDimension();
for (int i = 0; i < n; i++) {
final double y = v.getEntry(i);
if (Double.isNaN(y)) {
res.setEntry(i, Double.NaN);
} else if (Double.isInfinite(y)) {
final double x = this.getEntry(i);
res.setEntry(i, x * y);
}
}
}
<CHANGEE>
<FILEE>
<FILEB>
@Override
public double dotProduct(RealVector v) {
if(v instanceof OpenMapRealVector) {
return dotProduct((OpenMapRealVector)v);
} else {
return super.dotProduct(v);
}
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeDivide(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
/*
* MATH-803: it is not sufficient to loop through non zero entries of
* this only. Indeed, if this[i] = 0d and v[i] = 0d, then
* this[i] / v[i] = NaN, and not 0d.
*/
<CHANGES>
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() / v.getEntry(iter.key()));
<CHANGEE>
}
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeMultiply(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() * v.getEntry(iter.key()));
}
/*
* MATH-803: the above loop assumes that 0d * x = 0d for any double x,
* which allows to consider only the non-zero entries of this. However,
* this fails if this[i] == 0d and (v[i] = NaN or v[i] = Infinity).
*
* These special cases are handled below.
*/
<CHANGES>
<CHANGEE>
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector getSubVector(int index, int n) {
checkIndex(index);
if (n < 0) {
throw new NotPositiveException(LocalizedFormats.NUMBER_OF_ELEMENTS_SHOULD_BE_POSITIVE, n);
}
checkIndex(index + n - 1);
OpenMapRealVector<SCANS>56692756620;
if (hiPrec != null) {
hiPrec[0] /= 4.455505956692756620;
hiPrec[1] /= 4.455505956692756620;
}
return result;
}
intVal++;
intPartA = ExpIntTable.EXP_INT_TABLE_A[EXP_INT_TABLE_MAX_INDEX-intVal];
intPartB = ExpIntTable.EXP_INT_TABLE_B[EXP_INT_TABLE_MAX_INDEX-intVal];
intVal = -intVal;
} else {
intVal = (int) x;
if (intVal > 709) {
if (hiPrec != null) {
hiPrec[0] = Double.POSITIVE_INFINITY;
hiPrec[1] = 0.0;
}
return Double.POSITIVE_INFINITY;
}
intPartA = ExpIntTable.EXP_INT_TABLE_A[EXP_INT_TABLE_MAX_INDEX+intVal];
intPartB = ExpIntTable.EXP_INT_TABLE_B[EXP_INT_TABLE_MAX_INDEX+intVal];
}
/* Get the fractional part of x, find the greatest multiple of 2^-10 less than
* x and look up the exp function of it.
* fracPartA will have the upper 22 bits, fracPartB the lower 52 bits.
*/
final int intFrac = (int) ((x - intVal) * 1024.0);
final double fracPartA = ExpFracTable.EXP_FRAC_TABLE_A[intFrac];
final double fracPartB = ExpFracTable.EXP_FRAC_TABLE_B[intFrac];
/* epsilon is the difference in x from the nearest multiple of 2^-10. It
* has a value in the range 0 <= epsilon < 2^-10.
* Do the subtraction from x as the last step to avoid possible loss of percison.
*/
final double epsilon = x - (intVal + intFrac / 1024.0
Math, 29
<FILEB>
<CHANGES>
final int n = getDimension();
for (int i = 0; i < n; i++) {
res.setEntry(i, this.getEntry(i) / v.getEntry(i));
<CHANGEE>
<CHANGES>
if (v.isNaN() || v.isInfinite()) {
final int n = getDimension();
for (int i = 0; i < n; i++) {
final double y = v.getEntry(i);
if (Double.isNaN(y)) {
res.setEntry(i, Double.NaN);
} else if (Double.isInfinite(y)) {
final double x = this.getEntry(i);
res.setEntry(i, x * y);
}
}
}
<CHANGEE>
<FILEE>
<FILEB>
@Override
public double dotProduct(RealVector v) {
if(v instanceof OpenMapRealVector) {
return dotProduct((OpenMapRealVector)v);
} else {
return super.dotProduct(v);
}
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeDivide(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
/*
* MATH-803: it is not sufficient to loop through non zero entries of
* this only. Indeed, if this[i] = 0d and v[i] = 0d, then
* this[i] / v[i] = NaN, and not 0d.
*/
<CHANGES>
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() / v.getEntry(iter.key()));
<CHANGEE>
}
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeMultiply(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() * v.getEntry(iter.key()));
}
/*
* MATH-803: the above loop assumes that 0d * x = 0d for any double x,
* which allows to consider only the non-zero entries of this. However,
* this fails if this[i] == 0d and (v[i] = NaN or v[i] = Infinity).
*
* These special cases are handled below.
*/
<CHANGES>
<CHANGEE>
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector getSubVector(int index, int n) {
checkIndex(index);
if (n < 0) {
throw new NotPositiveException(LocalizedFormats.NUMBER_OF_ELEMENTS_SHOULD_BE_POSITIVE, n);
}
checkIndex(index + n - 1);
OpenMapRealVector<SCANS>1] = tempC*extra*z + tempC*extra + tempC*z + tempB;
}
return result;
}
/** Compute exp(x) - 1
* @param x number to compute shifted exponential
* @return exp(x) - 1
*/
public static double expm1(double x) {
return expm1(x, null);
}
/** Internal helper method for expm1
* @param x number to compute shifted exponential
* @param hiPrecOut receive high precision result for -1.0 < x < 1.0
* @return exp(x) - 1
*/
private static double expm1(double x, double hiPrecOut[]) {
if (x != x || x == 0.0) { // NaN or zero
return x;
}
if (x <= -1.0 || x >= 1.0) {
// If not between +/- 1.0
//return exp(x) - 1.0;
double hiPrec[] = new double[2];
exp(x, 0.0, hiPrec);
if (x > 0.0) {
return -1.0 + hiPrec[0] + hiPrec[1];
} else {
final double ra = -1.0 + hiPrec[0];
double rb = -(ra + 1.0 - hiPrec[0]);
rb += hiPrec[1];
return ra + rb;
}
}
double baseA;
double baseB;
double epsilon;
boolean negative = false;
if (x < 0.0) {
x = -x;
negative = true;
}
{
int intFrac = (int) (x * 1024.0);
double tempA = ExpFracTable.EXP_FRAC_TABLE_A[intFrac] - 1.0;
double tempB = ExpFracTable.EXP_FRAC_TABLE_B[intFrac];
double temp = tempA + tempB;
tempB = -(temp - tempA - tempB);
tempA = temp;
temp = tempA * HEX_40000000;
baseA = tempA + temp - temp;
baseB = temp
Math, 29
<FILEB>
<CHANGES>
final int n = getDimension();
for (int i = 0; i < n; i++) {
res.setEntry(i, this.getEntry(i) / v.getEntry(i));
<CHANGEE>
<CHANGES>
if (v.isNaN() || v.isInfinite()) {
final int n = getDimension();
for (int i = 0; i < n; i++) {
final double y = v.getEntry(i);
if (Double.isNaN(y)) {
res.setEntry(i, Double.NaN);
} else if (Double.isInfinite(y)) {
final double x = this.getEntry(i);
res.setEntry(i, x * y);
}
}
}
<CHANGEE>
<FILEE>
<FILEB>
@Override
public double dotProduct(RealVector v) {
if(v instanceof OpenMapRealVector) {
return dotProduct((OpenMapRealVector)v);
} else {
return super.dotProduct(v);
}
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeDivide(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
/*
* MATH-803: it is not sufficient to loop through non zero entries of
* this only. Indeed, if this[i] = 0d and v[i] = 0d, then
* this[i] / v[i] = NaN, and not 0d.
*/
<CHANGES>
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() / v.getEntry(iter.key()));
<CHANGEE>
}
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeMultiply(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() * v.getEntry(iter.key()));
}
/*
* MATH-803: the above loop assumes that 0d * x = 0d for any double x,
* which allows to consider only the non-zero entries of this. However,
* this fails if this[i] == 0d and (v[i] = NaN or v[i] = Infinity).
*
* These special cases are handled below.
*/
<CHANGES>
<CHANGEE>
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector getSubVector(int index, int n) {
checkIndex(index);
if (n < 0) {
throw new NotPositiveException(LocalizedFormats.NUMBER_OF_ELEMENTS_SHOULD_BE_POSITIVE, n);
}
checkIndex(index + n - 1);
OpenMapRealVector<SCANS>B + (tempA - baseA);
epsilon = x - intFrac/1024.0;
}
/* Compute expm1(epsilon) */
double zb = 0.008336750013465571;
zb = zb * epsilon + 0.041666663879186654;
zb = zb * epsilon + 0.16666666666745392;
zb = zb * epsilon + 0.49999999999999994;
zb = zb * epsilon;
zb = zb * epsilon;
double za = epsilon;
double temp = za + zb;
zb = -(temp - za - zb);
za = temp;
temp = za * HEX_40000000;
temp = za + temp - temp;
zb += za - temp;
za = temp;
/* Combine the parts. expm1(a+b) = expm1(a) + expm1(b) + expm1(a)*expm1(b) */
double ya = za * baseA;
//double yb = za*baseB + zb*baseA + zb*baseB;
temp = ya + za * baseB;
double yb = -(temp - ya - za * baseB);
ya = temp;
temp = ya + zb * baseA;
yb += -(temp - ya - zb * baseA);
ya = temp;
temp = ya + zb * baseB;
yb += -(temp - ya - zb*baseB);
ya = temp;
//ya = ya + za + baseA;
//yb = yb + zb + baseB;
temp = ya + baseA;
yb += -(temp - baseA - ya);
ya = temp;
temp = ya + za;
//yb += (ya > za) ? -(temp - ya - za) : -(temp - za - ya);
yb += -(temp - ya - za);
ya = temp;
temp =
Math, 29
<FILEB>
<CHANGES>
final int n = getDimension();
for (int i = 0; i < n; i++) {
res.setEntry(i, this.getEntry(i) / v.getEntry(i));
<CHANGEE>
<CHANGES>
if (v.isNaN() || v.isInfinite()) {
final int n = getDimension();
for (int i = 0; i < n; i++) {
final double y = v.getEntry(i);
if (Double.isNaN(y)) {
res.setEntry(i, Double.NaN);
} else if (Double.isInfinite(y)) {
final double x = this.getEntry(i);
res.setEntry(i, x * y);
}
}
}
<CHANGEE>
<FILEE>
<FILEB>
@Override
public double dotProduct(RealVector v) {
if(v instanceof OpenMapRealVector) {
return dotProduct((OpenMapRealVector)v);
} else {
return super.dotProduct(v);
}
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeDivide(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
/*
* MATH-803: it is not sufficient to loop through non zero entries of
* this only. Indeed, if this[i] = 0d and v[i] = 0d, then
* this[i] / v[i] = NaN, and not 0d.
*/
<CHANGES>
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() / v.getEntry(iter.key()));
<CHANGEE>
}
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeMultiply(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() * v.getEntry(iter.key()));
}
/*
* MATH-803: the above loop assumes that 0d * x = 0d for any double x,
* which allows to consider only the non-zero entries of this. However,
* this fails if this[i] == 0d and (v[i] = NaN or v[i] = Infinity).
*
* These special cases are handled below.
*/
<CHANGES>
<CHANGEE>
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector getSubVector(int index, int n) {
checkIndex(index);
if (n < 0) {
throw new NotPositiveException(LocalizedFormats.NUMBER_OF_ELEMENTS_SHOULD_BE_POSITIVE, n);
}
checkIndex(index + n - 1);
OpenMapRealVector<SCANS> != null) {
hiPrecOut[0] = ya;
hiPrecOut[1] = yb;
}
return ya + yb;
}
/**
* Natural logarithm.
*
* @param x a double
* @return log(x)
*/
public static double log(final double x) {
return log(x, null);
}
/**
* Internal helper method for natural logarithm function.
* @param x original argument of the natural logarithm function
* @param hiPrec extra bits of precision on output (To Be Confirmed)
* @return log(x)
*/
private static double log(final double x, final double[] hiPrec) {
if (x==0) { // Handle special case of +0/-0
return Double.NEGATIVE_INFINITY;
}
long bits = Double.doubleToLongBits(x);
/* Handle special cases of negative input, and NaN */
if ((bits & 0x8000000000000000L) != 0 || x != x) {
if (x != 0.0) {
if (hiPrec != null) {
hiPrec[0] = Double.NaN;
}
return Double.NaN;
}
}
/* Handle special cases of Positive infinity. */
if (x == Double.POSITIVE_INFINITY) {
if (hiPrec != null) {
hiPrec[0] = Double.POSITIVE_INFINITY;
}
return Double.POSITIVE_INFINITY;
}
/* Extract the exponent */
int exp = (int)(bits >> 52)-1023;
if ((bits & 0x7ff0000000000000L) == 0) {
// Subnormal!
if (x == 0) {
// Zero
if (hiPrec != null) {
hiPrec[0] = Double.NEGATIVE_INFINITY;
}
return Double.NEGATIVE_INFINITY;
}
/* Normalize the subnormal number. */
bits <<= 1;
while ( (bits & 0x0010000000000000L) == 0) {
exp--;
bits <<= 1;
}
Math, 29
<FILEB>
<CHANGES>
final int n = getDimension();
for (int i = 0; i < n; i++) {
res.setEntry(i, this.getEntry(i) / v.getEntry(i));
<CHANGEE>
<CHANGES>
if (v.isNaN() || v.isInfinite()) {
final int n = getDimension();
for (int i = 0; i < n; i++) {
final double y = v.getEntry(i);
if (Double.isNaN(y)) {
res.setEntry(i, Double.NaN);
} else if (Double.isInfinite(y)) {
final double x = this.getEntry(i);
res.setEntry(i, x * y);
}
}
}
<CHANGEE>
<FILEE>
<FILEB>
@Override
public double dotProduct(RealVector v) {
if(v instanceof OpenMapRealVector) {
return dotProduct((OpenMapRealVector)v);
} else {
return super.dotProduct(v);
}
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeDivide(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
/*
* MATH-803: it is not sufficient to loop through non zero entries of
* this only. Indeed, if this[i] = 0d and v[i] = 0d, then
* this[i] / v[i] = NaN, and not 0d.
*/
<CHANGES>
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() / v.getEntry(iter.key()));
<CHANGEE>
}
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeMultiply(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() * v.getEntry(iter.key()));
}
/*
* MATH-803: the above loop assumes that 0d * x = 0d for any double x,
* which allows to consider only the non-zero entries of this. However,
* this fails if this[i] == 0d and (v[i] = NaN or v[i] = Infinity).
*
* These special cases are handled below.
*/
<CHANGES>
<CHANGEE>
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector getSubVector(int index, int n) {
checkIndex(index);
if (n < 0) {
throw new NotPositiveException(LocalizedFormats.NUMBER_OF_ELEMENTS_SHOULD_BE_POSITIVE, n);
}
checkIndex(index + n - 1);
OpenMapRealVector<SCANS> }
if (exp == -1 || exp == 0) {
if (x < 1.01 && x > 0.99 && hiPrec == null) {
/* The normal method doesn't work well in the range [0.99, 1.01], so call do a straight
polynomial expansion in higer precision. */
/* Compute x - 1.0 and split it */
double xa = x - 1.0;
double xb = xa - x + 1.0;
double tmp = xa * HEX_40000000;
double aa = xa + tmp - tmp;
double ab = xa - aa;
xa = aa;
xb = ab;
double ya = LN_QUICK_COEF[LN_QUICK_COEF.length-1][0];
double yb = LN_QUICK_COEF[LN_QUICK_COEF.length-1][1];
for (int i = LN_QUICK_COEF.length - 2; i >= 0; i--) {
/* Multiply a = y * x */
aa = ya * xa;
ab = ya * xb + yb * xa + yb * xb;
/* split, so now y = a */
tmp = aa * HEX_40000000;
ya = aa + tmp - tmp;
yb = aa - ya + ab;
/* Add a = y + lnQuickCoef */
aa = ya + LN_QUICK_COEF[i][0];
ab = yb + LN_QUICK_COEF[i][1];
/* Split y = a */
tmp = aa * HEX_40000000;
ya = aa + tmp - tmp;
yb = aa - ya + ab;
}
/* Multiply a = y * x */
aa = ya * xa;
ab = ya * xb + yb * xa + yb * xb;
/* split, so now y = a */
tmp = aa * HEX_40000000;
ya = aa + tmp - tmp;
yb = aa - ya + ab;
return ya + yb;
}
}
// lnm is
Math, 29
<FILEB>
<CHANGES>
final int n = getDimension();
for (int i = 0; i < n; i++) {
res.setEntry(i, this.getEntry(i) / v.getEntry(i));
<CHANGEE>
<CHANGES>
if (v.isNaN() || v.isInfinite()) {
final int n = getDimension();
for (int i = 0; i < n; i++) {
final double y = v.getEntry(i);
if (Double.isNaN(y)) {
res.setEntry(i, Double.NaN);
} else if (Double.isInfinite(y)) {
final double x = this.getEntry(i);
res.setEntry(i, x * y);
}
}
}
<CHANGEE>
<FILEE>
<FILEB>
@Override
public double dotProduct(RealVector v) {
if(v instanceof OpenMapRealVector) {
return dotProduct((OpenMapRealVector)v);
} else {
return super.dotProduct(v);
}
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeDivide(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
/*
* MATH-803: it is not sufficient to loop through non zero entries of
* this only. Indeed, if this[i] = 0d and v[i] = 0d, then
* this[i] / v[i] = NaN, and not 0d.
*/
<CHANGES>
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() / v.getEntry(iter.key()));
<CHANGEE>
}
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeMultiply(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() * v.getEntry(iter.key()));
}
/*
* MATH-803: the above loop assumes that 0d * x = 0d for any double x,
* which allows to consider only the non-zero entries of this. However,
* this fails if this[i] == 0d and (v[i] = NaN or v[i] = Infinity).
*
* These special cases are handled below.
*/
<CHANGES>
<CHANGEE>
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector getSubVector(int index, int n) {
checkIndex(index);
if (n < 0) {
throw new NotPositiveException(LocalizedFormats.NUMBER_OF_ELEMENTS_SHOULD_BE_POSITIVE, n);
}
checkIndex(index + n - 1);
OpenMapRealVector<SCANS> a log of a number in the range of 1.0 - 2.0, so 0 <= lnm < ln(2)
double lnm[] = lnMant.LN_MANT[(int)((bits & 0x000ffc0000000000L) >> 42)];
/*
double epsilon = x / Double.longBitsToDouble(bits & 0xfffffc0000000000L);
epsilon -= 1.0;
*/
// y is the most significant 10 bits of the mantissa
//double y = Double.longBitsToDouble(bits & 0xfffffc0000000000L);
//double epsilon = (x - y) / y;
double epsilon = (bits & 0x3ffffffffffL) / (TWO_POWER_52 + (bits & 0x000ffc0000000000L));
double lnza = 0.0;
double lnzb = 0.0;
if (hiPrec != null) {
/* split epsilon -> x */
double tmp = epsilon * HEX_40000000;
double aa = epsilon + tmp - tmp;
double ab = epsilon - aa;
double xa = aa;
double xb = ab;
/* Need a more accurate epsilon, so adjust the division. */
double numer = bits & 0x3ffffffffffL;
double denom = TWO_POWER_52 + (bits & 0x000ffc0000000000L);
aa = numer - xa*denom - xb * denom;
xb += aa / denom;
/* Remez polynomial evaluation */
double ya = LN_HI_PREC_COEF[LN_HI_PREC_COEF.length-1][0];
double yb = LN_HI_PREC_COEF[LN_HI_PREC_COEF.length-1][1];
for (int i = LN_HI_PREC_COEF.length - 2; i >= 0; i--) {
/* Multiply a = y * x */
aa = ya * xa;
ab = ya * xb + yb * xa + y
Math, 29
<FILEB>
<CHANGES>
final int n = getDimension();
for (int i = 0; i < n; i++) {
res.setEntry(i, this.getEntry(i) / v.getEntry(i));
<CHANGEE>
<CHANGES>
if (v.isNaN() || v.isInfinite()) {
final int n = getDimension();
for (int i = 0; i < n; i++) {
final double y = v.getEntry(i);
if (Double.isNaN(y)) {
res.setEntry(i, Double.NaN);
} else if (Double.isInfinite(y)) {
final double x = this.getEntry(i);
res.setEntry(i, x * y);
}
}
}
<CHANGEE>
<FILEE>
<FILEB>
@Override
public double dotProduct(RealVector v) {
if(v instanceof OpenMapRealVector) {
return dotProduct((OpenMapRealVector)v);
} else {
return super.dotProduct(v);
}
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeDivide(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
/*
* MATH-803: it is not sufficient to loop through non zero entries of
* this only. Indeed, if this[i] = 0d and v[i] = 0d, then
* this[i] / v[i] = NaN, and not 0d.
*/
<CHANGES>
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() / v.getEntry(iter.key()));
<CHANGEE>
}
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeMultiply(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() * v.getEntry(iter.key()));
}
/*
* MATH-803: the above loop assumes that 0d * x = 0d for any double x,
* which allows to consider only the non-zero entries of this. However,
* this fails if this[i] == 0d and (v[i] = NaN or v[i] = Infinity).
*
* These special cases are handled below.
*/
<CHANGES>
<CHANGEE>
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector getSubVector(int index, int n) {
checkIndex(index);
if (n < 0) {
throw new NotPositiveException(LocalizedFormats.NUMBER_OF_ELEMENTS_SHOULD_BE_POSITIVE, n);
}
checkIndex(index + n - 1);
OpenMapRealVector<SCANS>b * xb;
/* split, so now y = a */
tmp = aa * HEX_40000000;
ya = aa + tmp - tmp;
yb = aa - ya + ab;
/* Add a = y + lnHiPrecCoef */
aa = ya + LN_HI_PREC_COEF[i][0];
ab = yb + LN_HI_PREC_COEF[i][1];
/* Split y = a */
tmp = aa * HEX_40000000;
ya = aa + tmp - tmp;
yb = aa - ya + ab;
}
/* Multiply a = y * x */
aa = ya * xa;
ab = ya * xb + yb * xa + yb * xb;
/* split, so now lnz = a */
/*
tmp = aa * 1073741824.0;
lnza = aa + tmp - tmp;
lnzb = aa - lnza + ab;
*/
lnza = aa + ab;
lnzb = -(lnza - aa - ab);
} else {
/* High precision not required. Eval Remez polynomial
using standard double precision */
lnza = -0.16624882440418567;
lnza = lnza * epsilon + 0.19999954120254515;
lnza = lnza * epsilon + -0.2499999997677497;
lnza = lnza * epsilon + 0.3333333333332802;
lnza = lnza * epsilon + -0.5;
lnza = lnza * epsilon + 1.0;
lnza = lnza * epsilon;
}
/* Relative sizes:
* lnzb [0, 2.33E-10]
* lnm[1] [0, 1.17E-7]
* ln2B*exp [0, 1.12E-4]
* lnza [0, 9.7E-4]
Math, 29
<FILEB>
<CHANGES>
final int n = getDimension();
for (int i = 0; i < n; i++) {
res.setEntry(i, this.getEntry(i) / v.getEntry(i));
<CHANGEE>
<CHANGES>
if (v.isNaN() || v.isInfinite()) {
final int n = getDimension();
for (int i = 0; i < n; i++) {
final double y = v.getEntry(i);
if (Double.isNaN(y)) {
res.setEntry(i, Double.NaN);
} else if (Double.isInfinite(y)) {
final double x = this.getEntry(i);
res.setEntry(i, x * y);
}
}
}
<CHANGEE>
<FILEE>
<FILEB>
@Override
public double dotProduct(RealVector v) {
if(v instanceof OpenMapRealVector) {
return dotProduct((OpenMapRealVector)v);
} else {
return super.dotProduct(v);
}
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeDivide(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
/*
* MATH-803: it is not sufficient to loop through non zero entries of
* this only. Indeed, if this[i] = 0d and v[i] = 0d, then
* this[i] / v[i] = NaN, and not 0d.
*/
<CHANGES>
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() / v.getEntry(iter.key()));
<CHANGEE>
}
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeMultiply(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() * v.getEntry(iter.key()));
}
/*
* MATH-803: the above loop assumes that 0d * x = 0d for any double x,
* which allows to consider only the non-zero entries of this. However,
* this fails if this[i] == 0d and (v[i] = NaN or v[i] = Infinity).
*
* These special cases are handled below.
*/
<CHANGES>
<CHANGEE>
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector getSubVector(int index, int n) {
checkIndex(index);
if (n < 0) {
throw new NotPositiveException(LocalizedFormats.NUMBER_OF_ELEMENTS_SHOULD_BE_POSITIVE, n);
}
checkIndex(index + n - 1);
OpenMapRealVector<SCANS>
* lnm[0] [0, 0.692]
* ln2A*exp [0, 709]
*/
/* Compute the following sum:
* lnzb + lnm[1] + ln2B*exp + lnza + lnm[0] + ln2A*exp;
*/
//return lnzb + lnm[1] + ln2B*exp + lnza + lnm[0] + ln2A*exp;
double a = LN_2_A*exp;
double b = 0.0;
double c = a+lnm[0];
double d = -(c-a-lnm[0]);
a = c;
b = b + d;
c = a + lnza;
d = -(c - a - lnza);
a = c;
b = b + d;
c = a + LN_2_B*exp;
d = -(c - a - LN_2_B*exp);
a = c;
b = b + d;
c = a + lnm[1];
d = -(c - a - lnm[1]);
a = c;
b = b + d;
c = a + lnzb;
d = -(c - a - lnzb);
a = c;
b = b + d;
if (hiPrec != null) {
hiPrec[0] = a;
hiPrec[1] = b;
}
return a + b;
}
/** Compute log(1 + x).
* @param x a number
* @return log(1 + x)
*/
public static double log1p(final double x) {
if (x == -1) {
return x/0.0; // -Infinity
}
if (x > 0 && 1/x == 0) { // x = Infinity
return x;
}
if (x>1e-6 || x<-1e-6) {
double xpa = 1.0 + x;
double xpb = -(xpa - 1.0 - x);
double hiPrec[] = new double[2];
final double lores = log(xpa, hiPrec);
Math, 29
<FILEB>
<CHANGES>
final int n = getDimension();
for (int i = 0; i < n; i++) {
res.setEntry(i, this.getEntry(i) / v.getEntry(i));
<CHANGEE>
<CHANGES>
if (v.isNaN() || v.isInfinite()) {
final int n = getDimension();
for (int i = 0; i < n; i++) {
final double y = v.getEntry(i);
if (Double.isNaN(y)) {
res.setEntry(i, Double.NaN);
} else if (Double.isInfinite(y)) {
final double x = this.getEntry(i);
res.setEntry(i, x * y);
}
}
}
<CHANGEE>
<FILEE>
<FILEB>
@Override
public double dotProduct(RealVector v) {
if(v instanceof OpenMapRealVector) {
return dotProduct((OpenMapRealVector)v);
} else {
return super.dotProduct(v);
}
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeDivide(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
/*
* MATH-803: it is not sufficient to loop through non zero entries of
* this only. Indeed, if this[i] = 0d and v[i] = 0d, then
* this[i] / v[i] = NaN, and not 0d.
*/
<CHANGES>
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() / v.getEntry(iter.key()));
<CHANGEE>
}
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeMultiply(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() * v.getEntry(iter.key()));
}
/*
* MATH-803: the above loop assumes that 0d * x = 0d for any double x,
* which allows to consider only the non-zero entries of this. However,
* this fails if this[i] == 0d and (v[i] = NaN or v[i] = Infinity).
*
* These special cases are handled below.
*/
<CHANGES>
<CHANGEE>
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector getSubVector(int index, int n) {
checkIndex(index);
if (n < 0) {
throw new NotPositiveException(LocalizedFormats.NUMBER_OF_ELEMENTS_SHOULD_BE_POSITIVE, n);
}
checkIndex(index + n - 1);
OpenMapRealVector<SCANS> if (Double.isInfinite(lores)){ // don't allow this to be converted to NaN
return lores;
}
/* Do a taylor series expansion around xpa */
/* f(x+y) = f(x) + f'(x)*y + f''(x)/2 y^2 */
double fx1 = xpb/xpa;
double epsilon = 0.5 * fx1 + 1.0;
epsilon = epsilon * fx1;
return epsilon + hiPrec[1] + hiPrec[0];
}
/* Value is small |x| < 1e6, do a Taylor series centered on 1.0 */
double y = x * F_1_3 - F_1_2;
y = y * x + 1.0;
y = y * x;
return y;
}
/** Compute the base 10 logarithm.
* @param x a number
* @return log10(x)
*/
public static double log10(final double x) {
final double hiPrec[] = new double[2];
final double lores = log(x, hiPrec);
if (Double.isInfinite(lores)){ // don't allow this to be converted to NaN
return lores;
}
final double tmp = hiPrec[0] * HEX_40000000;
final double lna = hiPrec[0] + tmp - tmp;
final double lnb = hiPrec[0] - lna + hiPrec[1];
final double rln10a = 0.4342944622039795;
final double rln10b = 1.9699272335463627E-8;
return rln10b * lnb + rln10b * lna + rln10a * lnb + rln10a * lna;
}
/**
* Computes the <a href="http://mathworld.wolfram.com/Logarithm.html">
* logarithm</a> in a given base.
*
* Returns {@code NaN} if either argument is negative.
* If {@code base}
Math, 29
<FILEB>
<CHANGES>
final int n = getDimension();
for (int i = 0; i < n; i++) {
res.setEntry(i, this.getEntry(i) / v.getEntry(i));
<CHANGEE>
<CHANGES>
if (v.isNaN() || v.isInfinite()) {
final int n = getDimension();
for (int i = 0; i < n; i++) {
final double y = v.getEntry(i);
if (Double.isNaN(y)) {
res.setEntry(i, Double.NaN);
} else if (Double.isInfinite(y)) {
final double x = this.getEntry(i);
res.setEntry(i, x * y);
}
}
}
<CHANGEE>
<FILEE>
<FILEB>
@Override
public double dotProduct(RealVector v) {
if(v instanceof OpenMapRealVector) {
return dotProduct((OpenMapRealVector)v);
} else {
return super.dotProduct(v);
}
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeDivide(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
/*
* MATH-803: it is not sufficient to loop through non zero entries of
* this only. Indeed, if this[i] = 0d and v[i] = 0d, then
* this[i] / v[i] = NaN, and not 0d.
*/
<CHANGES>
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() / v.getEntry(iter.key()));
<CHANGEE>
}
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeMultiply(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() * v.getEntry(iter.key()));
}
/*
* MATH-803: the above loop assumes that 0d * x = 0d for any double x,
* which allows to consider only the non-zero entries of this. However,
* this fails if this[i] == 0d and (v[i] = NaN or v[i] = Infinity).
*
* These special cases are handled below.
*/
<CHANGES>
<CHANGEE>
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector getSubVector(int index, int n) {
checkIndex(index);
if (n < 0) {
throw new NotPositiveException(LocalizedFormats.NUMBER_OF_ELEMENTS_SHOULD_BE_POSITIVE, n);
}
checkIndex(index + n - 1);
OpenMapRealVector<SCANS> is 0 and {@code x} is positive, 0 is returned.
* If {@code base} is positive and {@code x} is 0,
* {@code Double.NEGATIVE_INFINITY} is returned.
* If both arguments are 0, the result is {@code NaN}.
*
* @param base Base of the logarithm, must be greater than 0.
* @param x Argument, must be greater than 0.
* @return the value of the logarithm, i.e. the number {@code y} such that
* <code>base<sup>y</sup> = x</code>.
* @since 1.2 (previously in {@code MathUtils}, moved as of version 3.0)
*/
public static double log(double base, double x) {
return log(x) / log(base);
}
/**
* Power function. Compute x^y.
*
* @param x a double
* @param y a double
* @return double
*/
public static double pow(double x, double y) {
final double lns[] = new double[2];
if (y == 0.0) {
return 1.0;
}
if (x != x) { // X is NaN
return x;
}
if (x == 0) {
long bits = Double.doubleToLongBits(x);
if ((bits & 0x8000000000000000L) != 0) {
// -zero
long yi = (long) y;
if (y < 0 && y == yi && (yi & 1) == 1) {
return Double.NEGATIVE_INFINITY;
}
if (y > 0 && y == yi && (yi & 1) == 1) {
return -0.0;
}
}
if (y < 0) {
return Double.POSITIVE_INFINITY;
}
if (y > 0) {
return 0.0;
}
return Double.NaN;
}
if (x == Double.POSITIVE_INFINITY) {
if (y != y) { // y is NaN
return y;
}
if (
Math, 29
<FILEB>
<CHANGES>
final int n = getDimension();
for (int i = 0; i < n; i++) {
res.setEntry(i, this.getEntry(i) / v.getEntry(i));
<CHANGEE>
<CHANGES>
if (v.isNaN() || v.isInfinite()) {
final int n = getDimension();
for (int i = 0; i < n; i++) {
final double y = v.getEntry(i);
if (Double.isNaN(y)) {
res.setEntry(i, Double.NaN);
} else if (Double.isInfinite(y)) {
final double x = this.getEntry(i);
res.setEntry(i, x * y);
}
}
}
<CHANGEE>
<FILEE>
<FILEB>
@Override
public double dotProduct(RealVector v) {
if(v instanceof OpenMapRealVector) {
return dotProduct((OpenMapRealVector)v);
} else {
return super.dotProduct(v);
}
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeDivide(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
/*
* MATH-803: it is not sufficient to loop through non zero entries of
* this only. Indeed, if this[i] = 0d and v[i] = 0d, then
* this[i] / v[i] = NaN, and not 0d.
*/
<CHANGES>
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() / v.getEntry(iter.key()));
<CHANGEE>
}
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeMultiply(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() * v.getEntry(iter.key()));
}
/*
* MATH-803: the above loop assumes that 0d * x = 0d for any double x,
* which allows to consider only the non-zero entries of this. However,
* this fails if this[i] == 0d and (v[i] = NaN or v[i] = Infinity).
*
* These special cases are handled below.
*/
<CHANGES>
<CHANGEE>
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector getSubVector(int index, int n) {
checkIndex(index);
if (n < 0) {
throw new NotPositiveException(LocalizedFormats.NUMBER_OF_ELEMENTS_SHOULD_BE_POSITIVE, n);
}
checkIndex(index + n - 1);
OpenMapRealVector<SCANS>y < 0.0) {
return 0.0;
} else {
return Double.POSITIVE_INFINITY;
}
}
if (y == Double.POSITIVE_INFINITY) {
if (x * x == 1.0) {
return Double.NaN;
}
if (x * x > 1.0) {
return Double.POSITIVE_INFINITY;
} else {
return 0.0;
}
}
if (x == Double.NEGATIVE_INFINITY) {
if (y != y) { // y is NaN
return y;
}
if (y < 0) {
long yi = (long) y;
if (y == yi && (yi & 1) == 1) {
return -0.0;
}
return 0.0;
}
if (y > 0) {
long yi = (long) y;
if (y == yi && (yi & 1) == 1) {
return Double.NEGATIVE_INFINITY;
}
return Double.POSITIVE_INFINITY;
}
}
if (y == Double.NEGATIVE_INFINITY) {
if (x * x == 1.0) {
return Double.NaN;
}
if (x * x < 1.0) {
return Double.POSITIVE_INFINITY;
} else {
return 0.0;
}
}
/* Handle special case x<0 */
if (x < 0) {
// y is an even integer in this case
if (y >= TWO_POWER_52 || y <= -TWO_POWER_52) {
return pow(-x, y);
}
if (y == (long) y) {
// If y is an integer
return ((long)y & 1) == 0 ? pow(-x, y) : -pow(-x, y);
} else {
return Double.NaN;
}
}
/* Split y into ya and yb such that y = ya+yb */
double ya;
double yb;
if (y < 8e298 && y > -8e298) {
double tmp1 = y * HEX_40000000;
ya = y + tmp
Math, 29
<FILEB>
<CHANGES>
final int n = getDimension();
for (int i = 0; i < n; i++) {
res.setEntry(i, this.getEntry(i) / v.getEntry(i));
<CHANGEE>
<CHANGES>
if (v.isNaN() || v.isInfinite()) {
final int n = getDimension();
for (int i = 0; i < n; i++) {
final double y = v.getEntry(i);
if (Double.isNaN(y)) {
res.setEntry(i, Double.NaN);
} else if (Double.isInfinite(y)) {
final double x = this.getEntry(i);
res.setEntry(i, x * y);
}
}
}
<CHANGEE>
<FILEE>
<FILEB>
@Override
public double dotProduct(RealVector v) {
if(v instanceof OpenMapRealVector) {
return dotProduct((OpenMapRealVector)v);
} else {
return super.dotProduct(v);
}
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeDivide(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
/*
* MATH-803: it is not sufficient to loop through non zero entries of
* this only. Indeed, if this[i] = 0d and v[i] = 0d, then
* this[i] / v[i] = NaN, and not 0d.
*/
<CHANGES>
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() / v.getEntry(iter.key()));
<CHANGEE>
}
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeMultiply(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() * v.getEntry(iter.key()));
}
/*
* MATH-803: the above loop assumes that 0d * x = 0d for any double x,
* which allows to consider only the non-zero entries of this. However,
* this fails if this[i] == 0d and (v[i] = NaN or v[i] = Infinity).
*
* These special cases are handled below.
*/
<CHANGES>
<CHANGEE>
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector getSubVector(int index, int n) {
checkIndex(index);
if (n < 0) {
throw new NotPositiveException(LocalizedFormats.NUMBER_OF_ELEMENTS_SHOULD_BE_POSITIVE, n);
}
checkIndex(index + n - 1);
OpenMapRealVector<SCANS>1 - tmp1;
yb = y - ya;
} else {
double tmp1 = y * 9.31322574615478515625E-10;
double tmp2 = tmp1 * 9.31322574615478515625E-10;
ya = (tmp1 + tmp2 - tmp1) * HEX_40000000 * HEX_40000000;
yb = y - ya;
}
/* Compute ln(x) */
final double lores = log(x, lns);
if (Double.isInfinite(lores)){ // don't allow this to be converted to NaN
return lores;
}
double lna = lns[0];
double lnb = lns[1];
/* resplit lns */
double tmp1 = lna * HEX_40000000;
double tmp2 = lna + tmp1 - tmp1;
lnb += lna - tmp2;
lna = tmp2;
// y*ln(x) = (aa+ab)
final double aa = lna * ya;
final double ab = lna * yb + lnb * ya + lnb * yb;
lna = aa+ab;
lnb = -(lna - aa - ab);
double z = 1.0 / 120.0;
z = z * lnb + (1.0 / 24.0);
z = z * lnb + (1.0 / 6.0);
z = z * lnb + 0.5;
z = z * lnb + 1.0;
z = z * lnb;
final double result = exp(lna, z, null);
//result = result + result * z;
return result;
}
/**
* Computes sin(x) - x, where |x| < 1/16.
* Use a Remez polynomial approximation.
* @param x a number smaller than 1/16
* @return sin(x) - x
*/
private static double
Math, 29
<FILEB>
<CHANGES>
final int n = getDimension();
for (int i = 0; i < n; i++) {
res.setEntry(i, this.getEntry(i) / v.getEntry(i));
<CHANGEE>
<CHANGES>
if (v.isNaN() || v.isInfinite()) {
final int n = getDimension();
for (int i = 0; i < n; i++) {
final double y = v.getEntry(i);
if (Double.isNaN(y)) {
res.setEntry(i, Double.NaN);
} else if (Double.isInfinite(y)) {
final double x = this.getEntry(i);
res.setEntry(i, x * y);
}
}
}
<CHANGEE>
<FILEE>
<FILEB>
@Override
public double dotProduct(RealVector v) {
if(v instanceof OpenMapRealVector) {
return dotProduct((OpenMapRealVector)v);
} else {
return super.dotProduct(v);
}
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeDivide(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
/*
* MATH-803: it is not sufficient to loop through non zero entries of
* this only. Indeed, if this[i] = 0d and v[i] = 0d, then
* this[i] / v[i] = NaN, and not 0d.
*/
<CHANGES>
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() / v.getEntry(iter.key()));
<CHANGEE>
}
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeMultiply(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() * v.getEntry(iter.key()));
}
/*
* MATH-803: the above loop assumes that 0d * x = 0d for any double x,
* which allows to consider only the non-zero entries of this. However,
* this fails if this[i] == 0d and (v[i] = NaN or v[i] = Infinity).
*
* These special cases are handled below.
*/
<CHANGES>
<CHANGEE>
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector getSubVector(int index, int n) {
checkIndex(index);
if (n < 0) {
throw new NotPositiveException(LocalizedFormats.NUMBER_OF_ELEMENTS_SHOULD_BE_POSITIVE, n);
}
checkIndex(index + n - 1);
OpenMapRealVector<SCANS> polySine(final double x)
{
double x2 = x*x;
double p = 2.7553817452272217E-6;
p = p * x2 + -1.9841269659586505E-4;
p = p * x2 + 0.008333333333329196;
p = p * x2 + -0.16666666666666666;
//p *= x2;
//p *= x;
p = p * x2 * x;
return p;
}
/**
* Computes cos(x) - 1, where |x| < 1/16.
* Use a Remez polynomial approximation.
* @param x a number smaller than 1/16
* @return cos(x) - 1
*/
private static double polyCosine(double x) {
double x2 = x*x;
double p = 2.479773539153719E-5;
p = p * x2 + -0.0013888888689039883;
p = p * x2 + 0.041666666666621166;
p = p * x2 + -0.49999999999999994;
p *= x2;
return p;
}
/**
* Compute sine over the first quadrant (0 < x < pi/2).
* Use combination of table lookup and rational polynomial expansion.
* @param xa number from which sine is requested
* @param xb extra bits for x (may be 0.0)
* @return sin(xa + xb)
*/
private static double sinQ(double xa, double xb) {
int idx = (int) ((xa * 8.0) + 0.5);
final double epsilon = xa - EIGHTHS[idx];
Math, 29
<FILEB>
<CHANGES>
final int n = getDimension();
for (int i = 0; i < n; i++) {
res.setEntry(i, this.getEntry(i) / v.getEntry(i));
<CHANGEE>
<CHANGES>
if (v.isNaN() || v.isInfinite()) {
final int n = getDimension();
for (int i = 0; i < n; i++) {
final double y = v.getEntry(i);
if (Double.isNaN(y)) {
res.setEntry(i, Double.NaN);
} else if (Double.isInfinite(y)) {
final double x = this.getEntry(i);
res.setEntry(i, x * y);
}
}
}
<CHANGEE>
<FILEE>
<FILEB>
@Override
public double dotProduct(RealVector v) {
if(v instanceof OpenMapRealVector) {
return dotProduct((OpenMapRealVector)v);
} else {
return super.dotProduct(v);
}
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeDivide(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
/*
* MATH-803: it is not sufficient to loop through non zero entries of
* this only. Indeed, if this[i] = 0d and v[i] = 0d, then
* this[i] / v[i] = NaN, and not 0d.
*/
<CHANGES>
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() / v.getEntry(iter.key()));
<CHANGEE>
}
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeMultiply(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() * v.getEntry(iter.key()));
}
/*
* MATH-803: the above loop assumes that 0d * x = 0d for any double x,
* which allows to consider only the non-zero entries of this. However,
* this fails if this[i] == 0d and (v[i] = NaN or v[i] = Infinity).
*
* These special cases are handled below.
*/
<CHANGES>
<CHANGEE>
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector getSubVector(int index, int n) {
checkIndex(index);
if (n < 0) {
throw new NotPositiveException(LocalizedFormats.NUMBER_OF_ELEMENTS_SHOULD_BE_POSITIVE, n);
}
checkIndex(index + n - 1);
OpenMapRealVector<SCANS> cosa - cosaa;
//double err = (sina - est*cosa)/cosa; // Correction for division rounding
double err = (sina - esta*cosaa - esta*cosab - estb*cosaa - estb*cosab)/cosa; // Correction for division rounding
err += sinb/cosa; // Change in est due to sinb
err += -sina * cosb / cosa / cosa; // Change in est due to cosb
if (xb != 0.0) {
// tan' = 1 + tan^2 cot' = -(1 + cot^2)
// Approximate impact of xb
double xbadj = xb + est*est*xb;
if (cotanFlag) {
xbadj = -xbadj;
}
err += xbadj;
}
return est+err;
}
/** Reduce the input argument using the Payne and Hanek method.
* This is good for all inputs 0.0 < x < inf
* Output is remainder after dividing by PI/2
* The result array should contain 3 numbers.
* result[0] is the integer portion, so mod 4 this gives the quadrant.
* result[1] is the upper bits of the remainder
* result[2] is the lower bits of the remainder
*
* @param x number to reduce
* @param result placeholder where to put the result
*/
private static void reducePayneHanek(double x, double result[])
{
/* Convert input double to bits */
long inbits = Double.doubleToLongBits(x);
int exponent = (int) ((inbits >> 52) & 0x7ff) - 1023;
/* Convert to fixed point representation */
inbits &= 0x000fffffffffffffL;
inbits |= 0x0010000000000000L;
/* Normalize input to be between 0.5 and 1.0 */
exponent++;
inbits <<= 11;
/* Based on the exponent, get a shifted copy of recip2pi */
long shpi0;
long
Math, 29
<FILEB>
<CHANGES>
final int n = getDimension();
for (int i = 0; i < n; i++) {
res.setEntry(i, this.getEntry(i) / v.getEntry(i));
<CHANGEE>
<CHANGES>
if (v.isNaN() || v.isInfinite()) {
final int n = getDimension();
for (int i = 0; i < n; i++) {
final double y = v.getEntry(i);
if (Double.isNaN(y)) {
res.setEntry(i, Double.NaN);
} else if (Double.isInfinite(y)) {
final double x = this.getEntry(i);
res.setEntry(i, x * y);
}
}
}
<CHANGEE>
<FILEE>
<FILEB>
@Override
public double dotProduct(RealVector v) {
if(v instanceof OpenMapRealVector) {
return dotProduct((OpenMapRealVector)v);
} else {
return super.dotProduct(v);
}
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeDivide(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
/*
* MATH-803: it is not sufficient to loop through non zero entries of
* this only. Indeed, if this[i] = 0d and v[i] = 0d, then
* this[i] / v[i] = NaN, and not 0d.
*/
<CHANGES>
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() / v.getEntry(iter.key()));
<CHANGEE>
}
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeMultiply(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() * v.getEntry(iter.key()));
}
/*
* MATH-803: the above loop assumes that 0d * x = 0d for any double x,
* which allows to consider only the non-zero entries of this. However,
* this fails if this[i] == 0d and (v[i] = NaN or v[i] = Infinity).
*
* These special cases are handled below.
*/
<CHANGES>
<CHANGEE>
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector getSubVector(int index, int n) {
checkIndex(index);
if (n < 0) {
throw new NotPositiveException(LocalizedFormats.NUMBER_OF_ELEMENTS_SHOULD_BE_POSITIVE, n);
}
checkIndex(index + n - 1);
OpenMapRealVector<SCANS> // Low bits
double sumA = tmpA + tmpB;
double sumB = -(sumA - tmpA - tmpB);
/* Multiply by PI/2 and return */
result[0] = intPart;
result[1] = sumA * 2.0;
result[2] = sumB * 2.0;
}
/**
* Sine function.
* @param x a number
* @return sin(x)
*/
public static double sin(double x) {
boolean negative = false;
int quadrant = 0;
double xa;
double xb = 0.0;
/* Take absolute value of the input */
xa = x;
if (x < 0) {
negative = true;
xa = -xa;
}
/* Check for zero and negative zero */
if (xa == 0.0) {
long bits = Double.doubleToLongBits(x);
if (bits < 0) {
return -0.0;
}
return 0.0;
}
if (xa != xa || xa == Double.POSITIVE_INFINITY) {
return Double.NaN;
}
/* Perform any argument reduction */
if (xa > 3294198.0) {
// PI * (2**20)
// Argument too big for CodyWaite reduction. Must use
// PayneHanek.
double reduceResults[] = new double[3];
reducePayneHanek(xa, reduceResults);
quadrant = ((int) reduceResults[0]) & 3;
xa = reduceResults[1];
xb = reduceResults[2];
} else if (xa > 1.5707963267948966) {
/* Inline the Cody/Waite reduction for performance */
// Estimate k
//k = (int)(xa / 1.5707963267948966);
int k = (int)(xa * 0.6366197723675814);
// Compute remainder
double remA;
double remB;
while (true) {
double a = -k *
Math, 29
<FILEB>
<CHANGES>
final int n = getDimension();
for (int i = 0; i < n; i++) {
res.setEntry(i, this.getEntry(i) / v.getEntry(i));
<CHANGEE>
<CHANGES>
if (v.isNaN() || v.isInfinite()) {
final int n = getDimension();
for (int i = 0; i < n; i++) {
final double y = v.getEntry(i);
if (Double.isNaN(y)) {
res.setEntry(i, Double.NaN);
} else if (Double.isInfinite(y)) {
final double x = this.getEntry(i);
res.setEntry(i, x * y);
}
}
}
<CHANGEE>
<FILEE>
<FILEB>
@Override
public double dotProduct(RealVector v) {
if(v instanceof OpenMapRealVector) {
return dotProduct((OpenMapRealVector)v);
} else {
return super.dotProduct(v);
}
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeDivide(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
/*
* MATH-803: it is not sufficient to loop through non zero entries of
* this only. Indeed, if this[i] = 0d and v[i] = 0d, then
* this[i] / v[i] = NaN, and not 0d.
*/
<CHANGES>
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() / v.getEntry(iter.key()));
<CHANGEE>
}
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeMultiply(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() * v.getEntry(iter.key()));
}
/*
* MATH-803: the above loop assumes that 0d * x = 0d for any double x,
* which allows to consider only the non-zero entries of this. However,
* this fails if this[i] == 0d and (v[i] = NaN or v[i] = Infinity).
*
* These special cases are handled below.
*/
<CHANGES>
<CHANGEE>
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector getSubVector(int index, int n) {
checkIndex(index);
if (n < 0) {
throw new NotPositiveException(LocalizedFormats.NUMBER_OF_ELEMENTS_SHOULD_BE_POSITIVE, n);
}
checkIndex(index + n - 1);
OpenMapRealVector<SCANS> 1.570796251296997;
remA = xa + a;
remB = -(remA - xa - a);
a = -k * 7.549789948768648E-8;
double b = remA;
remA = a + b;
remB += -(remA - b - a);
a = -k * 6.123233995736766E-17;
b = remA;
remA = a + b;
remB += -(remA - b - a);
if (remA > 0.0) {
break;
}
// Remainder is negative, so decrement k and try again.
// This should only happen if the input is very close
// to an even multiple of pi/2
k--;
}
quadrant = k & 3;
xa = remA;
xb = remB;
}
if (negative) {
quadrant ^= 2; // Flip bit 1
}
switch (quadrant) {
case 0:
return sinQ(xa, xb);
case 1:
return cosQ(xa, xb);
case 2:
return -sinQ(xa, xb);
case 3:
return -cosQ(xa, xb);
default:
return Double.NaN;
}
}
/**
* Cosine function
* @param x a number
* @return cos(x)
*/
public static double cos(double x) {
int quadrant = 0;
/* Take absolute value of the input */
double xa = x;
if (x < 0) {
xa = -xa;
}
if (xa != xa || xa == Double.POSITIVE_INFINITY) {
return Double.NaN;
}
/* Perform any argument reduction */
double xb = 0;
if (xa > 3294198.0) {
// PI * (2**20)
// Argument too big for CodyWaite reduction. Must use
// PayneHanek.
double reduceResults[] = new double
Math, 29
<FILEB>
<CHANGES>
final int n = getDimension();
for (int i = 0; i < n; i++) {
res.setEntry(i, this.getEntry(i) / v.getEntry(i));
<CHANGEE>
<CHANGES>
if (v.isNaN() || v.isInfinite()) {
final int n = getDimension();
for (int i = 0; i < n; i++) {
final double y = v.getEntry(i);
if (Double.isNaN(y)) {
res.setEntry(i, Double.NaN);
} else if (Double.isInfinite(y)) {
final double x = this.getEntry(i);
res.setEntry(i, x * y);
}
}
}
<CHANGEE>
<FILEE>
<FILEB>
@Override
public double dotProduct(RealVector v) {
if(v instanceof OpenMapRealVector) {
return dotProduct((OpenMapRealVector)v);
} else {
return super.dotProduct(v);
}
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeDivide(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
/*
* MATH-803: it is not sufficient to loop through non zero entries of
* this only. Indeed, if this[i] = 0d and v[i] = 0d, then
* this[i] / v[i] = NaN, and not 0d.
*/
<CHANGES>
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() / v.getEntry(iter.key()));
<CHANGEE>
}
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeMultiply(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() * v.getEntry(iter.key()));
}
/*
* MATH-803: the above loop assumes that 0d * x = 0d for any double x,
* which allows to consider only the non-zero entries of this. However,
* this fails if this[i] == 0d and (v[i] = NaN or v[i] = Infinity).
*
* These special cases are handled below.
*/
<CHANGES>
<CHANGEE>
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector getSubVector(int index, int n) {
checkIndex(index);
if (n < 0) {
throw new NotPositiveException(LocalizedFormats.NUMBER_OF_ELEMENTS_SHOULD_BE_POSITIVE, n);
}
checkIndex(index + n - 1);
OpenMapRealVector<SCANS> Double.NaN;
}
}
/**
* Tangent function
* @param x a number
* @return tan(x)
*/
public static double tan(double x) {
boolean negative = false;
int quadrant = 0;
/* Take absolute value of the input */
double xa = x;
if (x < 0) {
negative = true;
xa = -xa;
}
/* Check for zero and negative zero */
if (xa == 0.0) {
long bits = Double.doubleToLongBits(x);
if (bits < 0) {
return -0.0;
}
return 0.0;
}
if (xa != xa || xa == Double.POSITIVE_INFINITY) {
return Double.NaN;
}
/* Perform any argument reduction */
double xb = 0;
if (xa > 3294198.0) {
// PI * (2**20)
// Argument too big for CodyWaite reduction. Must use
// PayneHanek.
double reduceResults[] = new double[3];
reducePayneHanek(xa, reduceResults);
quadrant = ((int) reduceResults[0]) & 3;
xa = reduceResults[1];
xb = reduceResults[2];
} else if (xa > 1.5707963267948966) {
/* Inline the Cody/Waite reduction for performance */
// Estimate k
//k = (int)(xa / 1.5707963267948966);
int k = (int)(xa * 0.6366197723675814);
// Compute remainder
double remA;
double remB;
while (true) {
double a = -k * 1.570796251296997;
remA = xa + a;
remB = -(remA - xa - a);
a = -k * 7.549789948768648E-8;
double b = remA;
Math, 29
<FILEB>
<CHANGES>
final int n = getDimension();
for (int i = 0; i < n; i++) {
res.setEntry(i, this.getEntry(i) / v.getEntry(i));
<CHANGEE>
<CHANGES>
if (v.isNaN() || v.isInfinite()) {
final int n = getDimension();
for (int i = 0; i < n; i++) {
final double y = v.getEntry(i);
if (Double.isNaN(y)) {
res.setEntry(i, Double.NaN);
} else if (Double.isInfinite(y)) {
final double x = this.getEntry(i);
res.setEntry(i, x * y);
}
}
}
<CHANGEE>
<FILEE>
<FILEB>
@Override
public double dotProduct(RealVector v) {
if(v instanceof OpenMapRealVector) {
return dotProduct((OpenMapRealVector)v);
} else {
return super.dotProduct(v);
}
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeDivide(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
/*
* MATH-803: it is not sufficient to loop through non zero entries of
* this only. Indeed, if this[i] = 0d and v[i] = 0d, then
* this[i] / v[i] = NaN, and not 0d.
*/
<CHANGES>
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() / v.getEntry(iter.key()));
<CHANGEE>
}
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeMultiply(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() * v.getEntry(iter.key()));
}
/*
* MATH-803: the above loop assumes that 0d * x = 0d for any double x,
* which allows to consider only the non-zero entries of this. However,
* this fails if this[i] == 0d and (v[i] = NaN or v[i] = Infinity).
*
* These special cases are handled below.
*/
<CHANGES>
<CHANGEE>
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector getSubVector(int index, int n) {
checkIndex(index);
if (n < 0) {
throw new NotPositiveException(LocalizedFormats.NUMBER_OF_ELEMENTS_SHOULD_BE_POSITIVE, n);
}
checkIndex(index + n - 1);
OpenMapRealVector<SCANS> remA = a + b;
remB += -(remA - b - a);
a = -k * 6.123233995736766E-17;
b = remA;
remA = a + b;
remB += -(remA - b - a);
if (remA > 0.0) {
break;
}
// Remainder is negative, so decrement k and try again.
// This should only happen if the input is very close
// to an even multiple of pi/2
k--;
}
quadrant = k & 3;
xa = remA;
xb = remB;
}
if (xa > 1.5) {
// Accurracy suffers between 1.5 and PI/2
final double pi2a = 1.5707963267948966;
final double pi2b = 6.123233995736766E-17;
final double a = pi2a - xa;
double b = -(a - pi2a + xa);
b += pi2b - xb;
xa = a + b;
xb = -(xa - a - b);
quadrant ^= 1;
negative ^= true;
}
double result;
if ((quadrant & 1) == 0) {
result = tanQ(xa, xb, false);
} else {
result = -tanQ(xa, xb, true);
}
if (negative) {
result = -result;
}
return result;
}
/**
* Arctangent function
* @param x a number
* @return atan(x)
*/
public static double atan(double x) {
return atan(x, 0.0, false);
}
/** Internal helper function to compute arctangent.
* @param xa number from which arctangent is requested
* @param xb extra bits for x (may be 0.0)
* @param leftPlane if true, result angle must be put in the left half plane
* @return atan(xa + xb) (or angle
Math, 29
<FILEB>
<CHANGES>
final int n = getDimension();
for (int i = 0; i < n; i++) {
res.setEntry(i, this.getEntry(i) / v.getEntry(i));
<CHANGEE>
<CHANGES>
if (v.isNaN() || v.isInfinite()) {
final int n = getDimension();
for (int i = 0; i < n; i++) {
final double y = v.getEntry(i);
if (Double.isNaN(y)) {
res.setEntry(i, Double.NaN);
} else if (Double.isInfinite(y)) {
final double x = this.getEntry(i);
res.setEntry(i, x * y);
}
}
}
<CHANGEE>
<FILEE>
<FILEB>
@Override
public double dotProduct(RealVector v) {
if(v instanceof OpenMapRealVector) {
return dotProduct((OpenMapRealVector)v);
} else {
return super.dotProduct(v);
}
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeDivide(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
/*
* MATH-803: it is not sufficient to loop through non zero entries of
* this only. Indeed, if this[i] = 0d and v[i] = 0d, then
* this[i] / v[i] = NaN, and not 0d.
*/
<CHANGES>
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() / v.getEntry(iter.key()));
<CHANGEE>
}
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeMultiply(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() * v.getEntry(iter.key()));
}
/*
* MATH-803: the above loop assumes that 0d * x = 0d for any double x,
* which allows to consider only the non-zero entries of this. However,
* this fails if this[i] == 0d and (v[i] = NaN or v[i] = Infinity).
*
* These special cases are handled below.
*/
<CHANGES>
<CHANGEE>
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector getSubVector(int index, int n) {
checkIndex(index);
if (n < 0) {
throw new NotPositiveException(LocalizedFormats.NUMBER_OF_ELEMENTS_SHOULD_BE_POSITIVE, n);
}
checkIndex(index + n - 1);
OpenMapRealVector<SCANS>
// Result is in the left plane
final double pia = 1.5707963267948966 * 2;
final double pib = 6.123233995736766E-17 * 2;
za = pia - result;
zb = -(za - pia + result);
zb += pib - resultb;
result = za + zb;
resultb = -(result - za - zb);
}
if (negate ^ leftPlane) {
result = -result;
}
return result;
}
/**
* Two arguments arctangent function
* @param y ordinate
* @param x abscissa
* @return phase angle of point (x,y) between {@code -PI} and {@code PI}
*/
public static double atan2(double y, double x) {
if (x != x || y != y) {
return Double.NaN;
}
if (y == 0) {
final double result = x * y;
final double invx = 1d / x;
final double invy = 1d / y;
if (invx == 0) { // X is infinite
if (x > 0) {
return y; // return +/- 0.0
} else {
return copySign(Math.PI, y);
}
}
if (x < 0 || invx < 0) {
if (y < 0 || invy < 0) {
return -Math.PI;
} else {
return Math.PI;
}
} else {
return result;
}
}
// y cannot now be zero
if (y == Double.POSITIVE_INFINITY) {
if (x == Double.POSITIVE_INFINITY) {
return Math.PI * F_1_4;
}
if (x == Double.NEGATIVE_INFINITY) {
return Math.PI * F_3_4;
}
return Math.PI * F_1_2;
}
if (y == Double.NEGATIVE_INFINITY) {
if (x == Double.POSITIVE_INFINITY) {
return -Math.PI * F_1_4
Math, 29
<FILEB>
<CHANGES>
final int n = getDimension();
for (int i = 0; i < n; i++) {
res.setEntry(i, this.getEntry(i) / v.getEntry(i));
<CHANGEE>
<CHANGES>
if (v.isNaN() || v.isInfinite()) {
final int n = getDimension();
for (int i = 0; i < n; i++) {
final double y = v.getEntry(i);
if (Double.isNaN(y)) {
res.setEntry(i, Double.NaN);
} else if (Double.isInfinite(y)) {
final double x = this.getEntry(i);
res.setEntry(i, x * y);
}
}
}
<CHANGEE>
<FILEE>
<FILEB>
@Override
public double dotProduct(RealVector v) {
if(v instanceof OpenMapRealVector) {
return dotProduct((OpenMapRealVector)v);
} else {
return super.dotProduct(v);
}
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeDivide(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
/*
* MATH-803: it is not sufficient to loop through non zero entries of
* this only. Indeed, if this[i] = 0d and v[i] = 0d, then
* this[i] / v[i] = NaN, and not 0d.
*/
<CHANGES>
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() / v.getEntry(iter.key()));
<CHANGEE>
}
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeMultiply(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() * v.getEntry(iter.key()));
}
/*
* MATH-803: the above loop assumes that 0d * x = 0d for any double x,
* which allows to consider only the non-zero entries of this. However,
* this fails if this[i] == 0d and (v[i] = NaN or v[i] = Infinity).
*
* These special cases are handled below.
*/
<CHANGES>
<CHANGEE>
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector getSubVector(int index, int n) {
checkIndex(index);
if (n < 0) {
throw new NotPositiveException(LocalizedFormats.NUMBER_OF_ELEMENTS_SHOULD_BE_POSITIVE, n);
}
checkIndex(index + n - 1);
OpenMapRealVector<SCANS>;
}
if (x == Double.NEGATIVE_INFINITY) {
return -Math.PI * F_3_4;
}
return -Math.PI * F_1_2;
}
if (x == Double.POSITIVE_INFINITY) {
if (y > 0 || 1 / y > 0) {
return 0d;
}
if (y < 0 || 1 / y < 0) {
return -0d;
}
}
if (x == Double.NEGATIVE_INFINITY)
{
if (y > 0.0 || 1 / y > 0.0) {
return Math.PI;
}
if (y < 0 || 1 / y < 0) {
return -Math.PI;
}
}
// Neither y nor x can be infinite or NAN here
if (x == 0) {
if (y > 0 || 1 / y > 0) {
return Math.PI * F_1_2;
}
if (y < 0 || 1 / y < 0) {
return -Math.PI * F_1_2;
}
}
// Compute ratio r = y/x
final double r = y / x;
if (Double.isInfinite(r)) { // bypass calculations that can create NaN
return atan(r, 0, x < 0);
}
double ra = doubleHighPart(r);
double rb = r - ra;
// Split x
final double xa = doubleHighPart(x);
final double xb = x - xa;
rb += (y - ra * xa - ra * xb - rb * xa - rb * xb) / x;
final double temp = ra + rb;
rb = -(temp - ra - rb);
ra = temp;
if (ra == 0) { // Fix up the sign so atan works correctly
ra = copySign(0d, y);
}
// Call atan
final double result = atan(ra, rb, x < 0);
return result;
}
/** Compute the arc sine of a number.
* @param x number on which evaluation is done
* @return arc sine of x
*/
public static double asin(
Math, 29
<FILEB>
<CHANGES>
final int n = getDimension();
for (int i = 0; i < n; i++) {
res.setEntry(i, this.getEntry(i) / v.getEntry(i));
<CHANGEE>
<CHANGES>
if (v.isNaN() || v.isInfinite()) {
final int n = getDimension();
for (int i = 0; i < n; i++) {
final double y = v.getEntry(i);
if (Double.isNaN(y)) {
res.setEntry(i, Double.NaN);
} else if (Double.isInfinite(y)) {
final double x = this.getEntry(i);
res.setEntry(i, x * y);
}
}
}
<CHANGEE>
<FILEE>
<FILEB>
@Override
public double dotProduct(RealVector v) {
if(v instanceof OpenMapRealVector) {
return dotProduct((OpenMapRealVector)v);
} else {
return super.dotProduct(v);
}
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeDivide(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
/*
* MATH-803: it is not sufficient to loop through non zero entries of
* this only. Indeed, if this[i] = 0d and v[i] = 0d, then
* this[i] / v[i] = NaN, and not 0d.
*/
<CHANGES>
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() / v.getEntry(iter.key()));
<CHANGEE>
}
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeMultiply(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() * v.getEntry(iter.key()));
}
/*
* MATH-803: the above loop assumes that 0d * x = 0d for any double x,
* which allows to consider only the non-zero entries of this. However,
* this fails if this[i] == 0d and (v[i] = NaN or v[i] = Infinity).
*
* These special cases are handled below.
*/
<CHANGES>
<CHANGEE>
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector getSubVector(int index, int n) {
checkIndex(index);
if (n < 0) {
throw new NotPositiveException(LocalizedFormats.NUMBER_OF_ELEMENTS_SHOULD_BE_POSITIVE, n);
}
checkIndex(index + n - 1);
OpenMapRealVector<SCANS>double x) {
if (x != x) {
return Double.NaN;
}
if (x > 1.0 || x < -1.0) {
return Double.NaN;
}
if (x == 1.0) {
return Math.PI/2.0;
}
if (x == -1.0) {
return -Math.PI/2.0;
}
if (x == 0.0) { // Matches +/- 0.0; return correct sign
return x;
}
/* Compute asin(x) = atan(x/sqrt(1-x*x)) */
/* Split x */
double temp = x * HEX_40000000;
final double xa = x + temp - temp;
final double xb = x - xa;
/* Square it */
double ya = xa*xa;
double yb = xa*xb*2.0 + xb*xb;
/* Subtract from 1 */
ya = -ya;
yb = -yb;
double za = 1.0 + ya;
double zb = -(za - 1.0 - ya);
temp = za + yb;
zb += -(temp - za - yb);
za = temp;
/* Square root */
double y;
y = sqrt(za);
temp = y * HEX_40000000;
ya = y + temp - temp;
yb = y - ya;
/* Extend precision of sqrt */
yb += (za - ya*ya - 2*ya*yb - yb*yb) / (2.0*y);
/* Contribution of zb to sqrt */
double dx = zb / (2.0*y);
// Compute ratio r = x/y
double r = x/y;
temp = r * HEX_40000000;
double ra = r + temp - temp;
double rb = r - ra;
rb += (x - ra*ya - ra*yb - rb*ya - rb*yb) / y; // Correct for rounding in division
rb += -x * dx / y / y; // Add in effect additional bits of sqrt.
temp =
Math, 29
<FILEB>
<CHANGES>
final int n = getDimension();
for (int i = 0; i < n; i++) {
res.setEntry(i, this.getEntry(i) / v.getEntry(i));
<CHANGEE>
<CHANGES>
if (v.isNaN() || v.isInfinite()) {
final int n = getDimension();
for (int i = 0; i < n; i++) {
final double y = v.getEntry(i);
if (Double.isNaN(y)) {
res.setEntry(i, Double.NaN);
} else if (Double.isInfinite(y)) {
final double x = this.getEntry(i);
res.setEntry(i, x * y);
}
}
}
<CHANGEE>
<FILEE>
<FILEB>
@Override
public double dotProduct(RealVector v) {
if(v instanceof OpenMapRealVector) {
return dotProduct((OpenMapRealVector)v);
} else {
return super.dotProduct(v);
}
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeDivide(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
/*
* MATH-803: it is not sufficient to loop through non zero entries of
* this only. Indeed, if this[i] = 0d and v[i] = 0d, then
* this[i] / v[i] = NaN, and not 0d.
*/
<CHANGES>
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() / v.getEntry(iter.key()));
<CHANGEE>
}
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeMultiply(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() * v.getEntry(iter.key()));
}
/*
* MATH-803: the above loop assumes that 0d * x = 0d for any double x,
* which allows to consider only the non-zero entries of this. However,
* this fails if this[i] == 0d and (v[i] = NaN or v[i] = Infinity).
*
* These special cases are handled below.
*/
<CHANGES>
<CHANGEE>
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector getSubVector(int index, int n) {
checkIndex(index);
if (n < 0) {
throw new NotPositiveException(LocalizedFormats.NUMBER_OF_ELEMENTS_SHOULD_BE_POSITIVE, n);
}
checkIndex(index + n - 1);
OpenMapRealVector<SCANS> ra + rb;
rb = -(temp - ra - rb);
ra = temp;
return atan(ra, rb, false);
}
/** Compute the arc cosine of a number.
* @param x number on which evaluation is done
* @return arc cosine of x
*/
public static double acos(double x) {
if (x != x) {
return Double.NaN;
}
if (x > 1.0 || x < -1.0) {
return Double.NaN;
}
if (x == -1.0) {
return Math.PI;
}
if (x == 1.0) {
return 0.0;
}
if (x == 0) {
return Math.PI/2.0;
}
/* Compute acos(x) = atan(sqrt(1-x*x)/x) */
/* Split x */
double temp = x * HEX_40000000;
final double xa = x + temp - temp;
final double xb = x - xa;
/* Square it */
double ya = xa*xa;
double yb = xa*xb*2.0 + xb*xb;
/* Subtract from 1 */
ya = -ya;
yb = -yb;
double za = 1.0 + ya;
double zb = -(za - 1.0 - ya);
temp = za + yb;
zb += -(temp - za - yb);
za = temp;
/* Square root */
double y = sqrt(za);
temp = y * HEX_40000000;
ya = y + temp - temp;
yb = y - ya;
/* Extend precision of sqrt */
yb += (za - ya*ya - 2*ya*yb - yb*yb) / (2.0*y);
/* Contribution of zb to sqrt */
yb += zb / (2.0*y);
y = ya+yb;
yb = -(y - ya - yb);
// Compute ratio r = y/x
double r = y/x;
// Did r overflow?
if (Double.isInfinite(r)) { // x is effectively
Math, 29
<FILEB>
<CHANGES>
final int n = getDimension();
for (int i = 0; i < n; i++) {
res.setEntry(i, this.getEntry(i) / v.getEntry(i));
<CHANGEE>
<CHANGES>
if (v.isNaN() || v.isInfinite()) {
final int n = getDimension();
for (int i = 0; i < n; i++) {
final double y = v.getEntry(i);
if (Double.isNaN(y)) {
res.setEntry(i, Double.NaN);
} else if (Double.isInfinite(y)) {
final double x = this.getEntry(i);
res.setEntry(i, x * y);
}
}
}
<CHANGEE>
<FILEE>
<FILEB>
@Override
public double dotProduct(RealVector v) {
if(v instanceof OpenMapRealVector) {
return dotProduct((OpenMapRealVector)v);
} else {
return super.dotProduct(v);
}
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeDivide(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
/*
* MATH-803: it is not sufficient to loop through non zero entries of
* this only. Indeed, if this[i] = 0d and v[i] = 0d, then
* this[i] / v[i] = NaN, and not 0d.
*/
<CHANGES>
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() / v.getEntry(iter.key()));
<CHANGEE>
}
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeMultiply(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() * v.getEntry(iter.key()));
}
/*
* MATH-803: the above loop assumes that 0d * x = 0d for any double x,
* which allows to consider only the non-zero entries of this. However,
* this fails if this[i] == 0d and (v[i] = NaN or v[i] = Infinity).
*
* These special cases are handled below.
*/
<CHANGES>
<CHANGEE>
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector getSubVector(int index, int n) {
checkIndex(index);
if (n < 0) {
throw new NotPositiveException(LocalizedFormats.NUMBER_OF_ELEMENTS_SHOULD_BE_POSITIVE, n);
}
checkIndex(index + n - 1);
OpenMapRealVector<SCANS> zero
return Math.PI/2; // so return the appropriate value
}
double ra = doubleHighPart(r);
double rb = r - ra;
rb += (y - ra*xa - ra*xb - rb*xa - rb*xb) / x; // Correct for rounding in division
rb += yb / x; // Add in effect additional bits of sqrt.
temp = ra + rb;
rb = -(temp - ra - rb);
ra = temp;
return atan(ra, rb, x<0);
}
/** Compute the cubic root of a number.
* @param x number on which evaluation is done
* @return cubic root of x
*/
public static double cbrt(double x) {
/* Convert input double to bits */
long inbits = Double.doubleToLongBits(x);
int exponent = (int) ((inbits >> 52) & 0x7ff) - 1023;
boolean subnormal = false;
if (exponent == -1023) {
if (x == 0) {
return x;
}
/* Subnormal, so normalize */
subnormal = true;
x *= 1.8014398509481984E16; // 2^54
inbits = Double.doubleToLongBits(x);
exponent = (int) ((inbits >> 52) & 0x7ff) - 1023;
}
if (exponent == 1024) {
// Nan or infinity. Don't care which.
return x;
}
/* Divide the exponent by 3 */
int exp3 = exponent / 3;
/* p2 will be the nearest power of 2 to x with its exponent divided by 3 */
double p2 = Double.longBitsToDouble((inbits & 0x8000000000000000L) |
(long)(((exp3 + 1023) & 0x7ff)) << 52);
/* This will be a number between 1 and 2 */
final double mant = Double.longBitsToDouble((inbits & 0x000fffffffffffff
Math, 29
<FILEB>
<CHANGES>
final int n = getDimension();
for (int i = 0; i < n; i++) {
res.setEntry(i, this.getEntry(i) / v.getEntry(i));
<CHANGEE>
<CHANGES>
if (v.isNaN() || v.isInfinite()) {
final int n = getDimension();
for (int i = 0; i < n; i++) {
final double y = v.getEntry(i);
if (Double.isNaN(y)) {
res.setEntry(i, Double.NaN);
} else if (Double.isInfinite(y)) {
final double x = this.getEntry(i);
res.setEntry(i, x * y);
}
}
}
<CHANGEE>
<FILEE>
<FILEB>
@Override
public double dotProduct(RealVector v) {
if(v instanceof OpenMapRealVector) {
return dotProduct((OpenMapRealVector)v);
} else {
return super.dotProduct(v);
}
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeDivide(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
/*
* MATH-803: it is not sufficient to loop through non zero entries of
* this only. Indeed, if this[i] = 0d and v[i] = 0d, then
* this[i] / v[i] = NaN, and not 0d.
*/
<CHANGES>
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() / v.getEntry(iter.key()));
<CHANGEE>
}
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeMultiply(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() * v.getEntry(iter.key()));
}
/*
* MATH-803: the above loop assumes that 0d * x = 0d for any double x,
* which allows to consider only the non-zero entries of this. However,
* this fails if this[i] == 0d and (v[i] = NaN or v[i] = Infinity).
*
* These special cases are handled below.
*/
<CHANGES>
<CHANGEE>
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector getSubVector(int index, int n) {
checkIndex(index);
if (n < 0) {
throw new NotPositiveException(LocalizedFormats.NUMBER_OF_ELEMENTS_SHOULD_BE_POSITIVE, n);
}
checkIndex(index + n - 1);
OpenMapRealVector<SCANS>L) | 0x3ff0000000000000L);
/* Estimate the cube root of mant by polynomial */
double est = -0.010714690733195933;
est = est * mant + 0.0875862700108075;
est = est * mant + -0.3058015757857271;
est = est * mant + 0.7249995199969751;
est = est * mant + 0.5039018405998233;
est *= CBRTTWO[exponent % 3 + 2];
// est should now be good to about 15 bits of precision. Do 2 rounds of
// Newton's method to get closer, this should get us full double precision
// Scale down x for the purpose of doing newtons method. This avoids over/under flows.
final double xs = x / (p2*p2*p2);
est += (xs - est*est*est) / (3*est*est);
est += (xs - est*est*est) / (3*est*est);
// Do one round of Newton's method in extended precision to get the last bit right.
double temp = est * HEX_40000000;
double ya = est + temp - temp;
double yb = est - ya;
double za = ya * ya;
double zb = ya * yb * 2.0 + yb * yb;
temp = za * HEX_40000000;
double temp2 = za + temp - temp;
zb += za - temp2;
za = temp2;
zb = za * yb + ya * zb + zb * yb;
za = za * ya;
double na = xs - za;
double nb = -(na - xs + za);
nb -= zb;
est += (na+nb)/(3*est*est);
/* Scale by a power of two
Math, 29
<FILEB>
<CHANGES>
final int n = getDimension();
for (int i = 0; i < n; i++) {
res.setEntry(i, this.getEntry(i) / v.getEntry(i));
<CHANGEE>
<CHANGES>
if (v.isNaN() || v.isInfinite()) {
final int n = getDimension();
for (int i = 0; i < n; i++) {
final double y = v.getEntry(i);
if (Double.isNaN(y)) {
res.setEntry(i, Double.NaN);
} else if (Double.isInfinite(y)) {
final double x = this.getEntry(i);
res.setEntry(i, x * y);
}
}
}
<CHANGEE>
<FILEE>
<FILEB>
@Override
public double dotProduct(RealVector v) {
if(v instanceof OpenMapRealVector) {
return dotProduct((OpenMapRealVector)v);
} else {
return super.dotProduct(v);
}
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeDivide(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
/*
* MATH-803: it is not sufficient to loop through non zero entries of
* this only. Indeed, if this[i] = 0d and v[i] = 0d, then
* this[i] / v[i] = NaN, and not 0d.
*/
<CHANGES>
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() / v.getEntry(iter.key()));
<CHANGEE>
}
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeMultiply(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() * v.getEntry(iter.key()));
}
/*
* MATH-803: the above loop assumes that 0d * x = 0d for any double x,
* which allows to consider only the non-zero entries of this. However,
* this fails if this[i] == 0d and (v[i] = NaN or v[i] = Infinity).
*
* These special cases are handled below.
*/
<CHANGES>
<CHANGEE>
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector getSubVector(int index, int n) {
checkIndex(index);
if (n < 0) {
throw new NotPositiveException(LocalizedFormats.NUMBER_OF_ELEMENTS_SHOULD_BE_POSITIVE, n);
}
checkIndex(index + n - 1);
OpenMapRealVector<SCANS>, so this is exact. */
est *= p2;
if (subnormal) {
est *= 3.814697265625E-6; // 2^-18
}
return est;
}
/**
* Convert degrees to radians, with error of less than 0.5 ULP
* @param x angle in degrees
* @return x converted into radians
*/
public static double toRadians(double x)
{
if (Double.isInfinite(x) || x == 0.0) { // Matches +/- 0.0; return correct sign
return x;
}
// These are PI/180 split into high and low order bits
final double facta = 0.01745329052209854;
final double factb = 1.997844754509471E-9;
double xa = doubleHighPart(x);
double xb = x - xa;
double result = xb * factb + xb * facta + xa * factb + xa * facta;
if (result == 0) {
result = result * x; // ensure correct sign if calculation underflows
}
return result;
}
/**
* Convert radians to degrees, with error of less than 0.5 ULP
* @param x angle in radians
* @return x converted into degrees
*/
public static double toDegrees(double x)
{
if (Double.isInfinite(x) || x == 0.0) { // Matches +/- 0.0; return correct sign
return x;
}
// These are 180/PI split into high and low order bits
final double facta = 57.2957763671875;
final double factb = 3.145894820876798E-6;
double xa = doubleHighPart(x);
double xb = x - xa;
return xb * factb + xb * facta + xa * factb + xa * facta;
}
/**
* Absolute value.
Math, 29
<FILEB>
<CHANGES>
final int n = getDimension();
for (int i = 0; i < n; i++) {
res.setEntry(i, this.getEntry(i) / v.getEntry(i));
<CHANGEE>
<CHANGES>
if (v.isNaN() || v.isInfinite()) {
final int n = getDimension();
for (int i = 0; i < n; i++) {
final double y = v.getEntry(i);
if (Double.isNaN(y)) {
res.setEntry(i, Double.NaN);
} else if (Double.isInfinite(y)) {
final double x = this.getEntry(i);
res.setEntry(i, x * y);
}
}
}
<CHANGEE>
<FILEE>
<FILEB>
@Override
public double dotProduct(RealVector v) {
if(v instanceof OpenMapRealVector) {
return dotProduct((OpenMapRealVector)v);
} else {
return super.dotProduct(v);
}
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeDivide(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
/*
* MATH-803: it is not sufficient to loop through non zero entries of
* this only. Indeed, if this[i] = 0d and v[i] = 0d, then
* this[i] / v[i] = NaN, and not 0d.
*/
<CHANGES>
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() / v.getEntry(iter.key()));
<CHANGEE>
}
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeMultiply(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() * v.getEntry(iter.key()));
}
/*
* MATH-803: the above loop assumes that 0d * x = 0d for any double x,
* which allows to consider only the non-zero entries of this. However,
* this fails if this[i] == 0d and (v[i] = NaN or v[i] = Infinity).
*
* These special cases are handled below.
*/
<CHANGES>
<CHANGEE>
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector getSubVector(int index, int n) {
checkIndex(index);
if (n < 0) {
throw new NotPositiveException(LocalizedFormats.NUMBER_OF_ELEMENTS_SHOULD_BE_POSITIVE, n);
}
checkIndex(index + n - 1);
OpenMapRealVector<SCANS>
* @param x number from which absolute value is requested
* @return abs(x)
*/
public static int abs(final int x) {
return (x < 0) ? -x : x;
}
/**
* Absolute value.
* @param x number from which absolute value is requested
* @return abs(x)
*/
public static long abs(final long x) {
return (x < 0l) ? -x : x;
}
/**
* Absolute value.
* @param x number from which absolute value is requested
* @return abs(x)
*/
public static float abs(final float x) {
return (x < 0.0f) ? -x : (x == 0.0f) ? 0.0f : x; // -0.0 => +0.0
}
/**
* Absolute value.
* @param x number from which absolute value is requested
* @return abs(x)
*/
public static double abs(double x) {
return (x < 0.0) ? -x : (x == 0.0) ? 0.0 : x; // -0.0 => +0.0
}
/**
* Compute least significant bit (Unit in Last Position) for a number.
* @param x number from which ulp is requested
* @return ulp(x)
*/
public static double ulp(double x) {
if (Double.isInfinite(x)) {
return Double.POSITIVE_INFINITY;
}
return abs(x - Double.longBitsToDouble(Double.doubleToLongBits(x) ^ 1));
}
/**
* Compute least significant bit (Unit in Last Position) for a number.
* @param x number from which ulp is requested
* @return ulp(x)
*/
public static float ulp(float x) {
if (Float.isInfinite(x)) {
return Float.POSITIVE_INFINITY;
}
return abs(x - Float.intBitsToFloat(Float.floatToIntBits(x) ^ 1));
}
/**
* Multiply a double number by a power of 2.
* @param d number to multiply
* @param n power of 2
* @return d
Math, 29
<FILEB>
<CHANGES>
final int n = getDimension();
for (int i = 0; i < n; i++) {
res.setEntry(i, this.getEntry(i) / v.getEntry(i));
<CHANGEE>
<CHANGES>
if (v.isNaN() || v.isInfinite()) {
final int n = getDimension();
for (int i = 0; i < n; i++) {
final double y = v.getEntry(i);
if (Double.isNaN(y)) {
res.setEntry(i, Double.NaN);
} else if (Double.isInfinite(y)) {
final double x = this.getEntry(i);
res.setEntry(i, x * y);
}
}
}
<CHANGEE>
<FILEE>
<FILEB>
@Override
public double dotProduct(RealVector v) {
if(v instanceof OpenMapRealVector) {
return dotProduct((OpenMapRealVector)v);
} else {
return super.dotProduct(v);
}
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeDivide(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
/*
* MATH-803: it is not sufficient to loop through non zero entries of
* this only. Indeed, if this[i] = 0d and v[i] = 0d, then
* this[i] / v[i] = NaN, and not 0d.
*/
<CHANGES>
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() / v.getEntry(iter.key()));
<CHANGEE>
}
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeMultiply(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() * v.getEntry(iter.key()));
}
/*
* MATH-803: the above loop assumes that 0d * x = 0d for any double x,
* which allows to consider only the non-zero entries of this. However,
* this fails if this[i] == 0d and (v[i] = NaN or v[i] = Infinity).
*
* These special cases are handled below.
*/
<CHANGES>
<CHANGEE>
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector getSubVector(int index, int n) {
checkIndex(index);
if (n < 0) {
throw new NotPositiveException(LocalizedFormats.NUMBER_OF_ELEMENTS_SHOULD_BE_POSITIVE, n);
}
checkIndex(index + n - 1);
OpenMapRealVector<SCANS> × 2<sup>n</sup>
*/
public static double scalb(final double d, final int n) {
// first simple and fast handling when 2^n can be represented using normal numbers
if ((n > -1023) && (n < 1024)) {
return d * Double.longBitsToDouble(((long) (n + 1023)) << 52);
}
// handle special cases
if (Double.isNaN(d) || Double.isInfinite(d) || (d == 0)) {
return d;
}
if (n < -2098) {
return (d > 0) ? 0.0 : -0.0;
}
if (n > 2097) {
return (d > 0) ? Double.POSITIVE_INFINITY : Double.NEGATIVE_INFINITY;
}
// decompose d
final long bits = Double.doubleToLongBits(d);
final long sign = bits & 0x8000000000000000L;
int exponent = ((int) (bits >>> 52)) & 0x7ff;
long mantissa = bits & 0x000fffffffffffffL;
// compute scaled exponent
int scaledExponent = exponent + n;
if (n < 0) {
// we are really in the case n <= -1023
if (scaledExponent > 0) {
// both the input and the result are normal numbers, we only adjust the exponent
return Double.longBitsToDouble(sign | (((long) scaledExponent) << 52) | mantissa);
} else if (scaledExponent > -53) {
// the input is a normal number and the result is a subnormal number
// recover the hidden mantissa bit
mantissa = mantissa | (1L << 52);
// scales down complete mantissa, hence losing least significant bits
final long mostSignificantLostBit = mantissa & (1L << (-scaledExponent));
mantissa = mantissa >>> (1 - scaledExponent);
if (mostSignificantLostBit != 0) {
// we need to add 1
Math, 29
<FILEB>
<CHANGES>
final int n = getDimension();
for (int i = 0; i < n; i++) {
res.setEntry(i, this.getEntry(i) / v.getEntry(i));
<CHANGEE>
<CHANGES>
if (v.isNaN() || v.isInfinite()) {
final int n = getDimension();
for (int i = 0; i < n; i++) {
final double y = v.getEntry(i);
if (Double.isNaN(y)) {
res.setEntry(i, Double.NaN);
} else if (Double.isInfinite(y)) {
final double x = this.getEntry(i);
res.setEntry(i, x * y);
}
}
}
<CHANGEE>
<FILEE>
<FILEB>
@Override
public double dotProduct(RealVector v) {
if(v instanceof OpenMapRealVector) {
return dotProduct((OpenMapRealVector)v);
} else {
return super.dotProduct(v);
}
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeDivide(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
/*
* MATH-803: it is not sufficient to loop through non zero entries of
* this only. Indeed, if this[i] = 0d and v[i] = 0d, then
* this[i] / v[i] = NaN, and not 0d.
*/
<CHANGES>
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() / v.getEntry(iter.key()));
<CHANGEE>
}
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeMultiply(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() * v.getEntry(iter.key()));
}
/*
* MATH-803: the above loop assumes that 0d * x = 0d for any double x,
* which allows to consider only the non-zero entries of this. However,
* this fails if this[i] == 0d and (v[i] = NaN or v[i] = Infinity).
*
* These special cases are handled below.
*/
<CHANGES>
<CHANGEE>
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector getSubVector(int index, int n) {
checkIndex(index);
if (n < 0) {
throw new NotPositiveException(LocalizedFormats.NUMBER_OF_ELEMENTS_SHOULD_BE_POSITIVE, n);
}
checkIndex(index + n - 1);
OpenMapRealVector<SCANS> bit to round up the result
mantissa++;
}
return Double.longBitsToDouble(sign | mantissa);
} else {
// no need to compute the mantissa, the number scales down to 0
return (sign == 0L) ? 0.0 : -0.0;
}
} else {
// we are really in the case n >= 1024
if (exponent == 0) {
// the input number is subnormal, normalize it
while ((mantissa >>> 52) != 1) {
mantissa = mantissa << 1;
--scaledExponent;
}
++scaledExponent;
mantissa = mantissa & 0x000fffffffffffffL;
if (scaledExponent < 2047) {
return Double.longBitsToDouble(sign | (((long) scaledExponent) << 52) | mantissa);
} else {
return (sign == 0L) ? Double.POSITIVE_INFINITY : Double.NEGATIVE_INFINITY;
}
} else if (scaledExponent < 2047) {
return Double.longBitsToDouble(sign | (((long) scaledExponent) << 52) | mantissa);
} else {
return (sign == 0L) ? Double.POSITIVE_INFINITY : Double.NEGATIVE_INFINITY;
}
}
}
/**
* Multiply a float number by a power of 2.
* @param f number to multiply
* @param n power of 2
* @return f × 2<sup>n</sup>
*/
public static float scalb(final float f, final int n) {
// first simple and fast handling when 2^n can be represented using normal numbers
if ((n > -127) && (n < 128)) {
return f * Float.intBitsToFloat((n + 127) << 23);
}
// handle special cases
if (Float.isNaN(f) || Float.isInfinite(f) || (f == 0f)) {
return f;
}
if (n < -277) {
return (f > 0) ? 0.0f : -0.0f;
Math, 29
<FILEB>
<CHANGES>
final int n = getDimension();
for (int i = 0; i < n; i++) {
res.setEntry(i, this.getEntry(i) / v.getEntry(i));
<CHANGEE>
<CHANGES>
if (v.isNaN() || v.isInfinite()) {
final int n = getDimension();
for (int i = 0; i < n; i++) {
final double y = v.getEntry(i);
if (Double.isNaN(y)) {
res.setEntry(i, Double.NaN);
} else if (Double.isInfinite(y)) {
final double x = this.getEntry(i);
res.setEntry(i, x * y);
}
}
}
<CHANGEE>
<FILEE>
<FILEB>
@Override
public double dotProduct(RealVector v) {
if(v instanceof OpenMapRealVector) {
return dotProduct((OpenMapRealVector)v);
} else {
return super.dotProduct(v);
}
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeDivide(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
/*
* MATH-803: it is not sufficient to loop through non zero entries of
* this only. Indeed, if this[i] = 0d and v[i] = 0d, then
* this[i] / v[i] = NaN, and not 0d.
*/
<CHANGES>
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() / v.getEntry(iter.key()));
<CHANGEE>
}
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeMultiply(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() * v.getEntry(iter.key()));
}
/*
* MATH-803: the above loop assumes that 0d * x = 0d for any double x,
* which allows to consider only the non-zero entries of this. However,
* this fails if this[i] == 0d and (v[i] = NaN or v[i] = Infinity).
*
* These special cases are handled below.
*/
<CHANGES>
<CHANGEE>
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector getSubVector(int index, int n) {
checkIndex(index);
if (n < 0) {
throw new NotPositiveException(LocalizedFormats.NUMBER_OF_ELEMENTS_SHOULD_BE_POSITIVE, n);
}
checkIndex(index + n - 1);
OpenMapRealVector<SCANS>sign | (scaledExponent << 23) | mantissa);
} else {
return (sign == 0) ? Float.POSITIVE_INFINITY : Float.NEGATIVE_INFINITY;
}
} else if (scaledExponent < 255) {
return Float.intBitsToFloat(sign | (scaledExponent << 23) | mantissa);
} else {
return (sign == 0) ? Float.POSITIVE_INFINITY : Float.NEGATIVE_INFINITY;
}
}
}
/**
* Get the next machine representable number after a number, moving
* in the direction of another number.
* <p>
* The ordering is as follows (increasing):
* <ul>
* <li>-INFINITY</li>
* <li>-MAX_VALUE</li>
* <li>-MIN_VALUE</li>
* <li>-0.0</li>
* <li>+0.0</li>
* <li>+MIN_VALUE</li>
* <li>+MAX_VALUE</li>
* <li>+INFINITY</li>
* <li></li>
* <p>
* If arguments compare equal, then the second argument is returned.
* <p>
* If {@code direction} is greater than {@code d},
* the smallest machine representable number strictly greater than
* {@code d} is returned; if less, then the largest representable number
* strictly less than {@code d} is returned.</p>
* <p>
* If {@code d} is infinite and direction does not
* bring it back to finite numbers, it is returned unchanged.</p>
*
* @param d base number
* @param direction (the only important thing is whether
* {@code direction} is greater or smaller than {@code d})
* @return the next machine representable number in the specified direction
*/
public static double nextAfter(double d, double direction) {
// handling of some important special cases
if (Double.isNaN(d) || Double.isNaN(direction)) {
return Double.NaN;
} else if (d == direction) {
return direction;
} else if (Double.isInfinite(d)) {
return (d < 0) ? -Double.MAX_VALUE : Double.
Math, 29
<FILEB>
<CHANGES>
final int n = getDimension();
for (int i = 0; i < n; i++) {
res.setEntry(i, this.getEntry(i) / v.getEntry(i));
<CHANGEE>
<CHANGES>
if (v.isNaN() || v.isInfinite()) {
final int n = getDimension();
for (int i = 0; i < n; i++) {
final double y = v.getEntry(i);
if (Double.isNaN(y)) {
res.setEntry(i, Double.NaN);
} else if (Double.isInfinite(y)) {
final double x = this.getEntry(i);
res.setEntry(i, x * y);
}
}
}
<CHANGEE>
<FILEE>
<FILEB>
@Override
public double dotProduct(RealVector v) {
if(v instanceof OpenMapRealVector) {
return dotProduct((OpenMapRealVector)v);
} else {
return super.dotProduct(v);
}
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeDivide(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
/*
* MATH-803: it is not sufficient to loop through non zero entries of
* this only. Indeed, if this[i] = 0d and v[i] = 0d, then
* this[i] / v[i] = NaN, and not 0d.
*/
<CHANGES>
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() / v.getEntry(iter.key()));
<CHANGEE>
}
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeMultiply(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() * v.getEntry(iter.key()));
}
/*
* MATH-803: the above loop assumes that 0d * x = 0d for any double x,
* which allows to consider only the non-zero entries of this. However,
* this fails if this[i] == 0d and (v[i] = NaN or v[i] = Infinity).
*
* These special cases are handled below.
*/
<CHANGES>
<CHANGEE>
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector getSubVector(int index, int n) {
checkIndex(index);
if (n < 0) {
throw new NotPositiveException(LocalizedFormats.NUMBER_OF_ELEMENTS_SHOULD_BE_POSITIVE, n);
}
checkIndex(index + n - 1);
OpenMapRealVector<SCANS>MAX_VALUE;
} else if (d == 0) {
return (direction < 0) ? -Double.MIN_VALUE : Double.MIN_VALUE;
}
// special cases MAX_VALUE to infinity and MIN_VALUE to 0
// are handled just as normal numbers
final long bits = Double.doubleToLongBits(d);
final long sign = bits & 0x8000000000000000L;
if ((direction < d) ^ (sign == 0L)) {
return Double.longBitsToDouble(sign | ((bits & 0x7fffffffffffffffL) + 1));
} else {
return Double.longBitsToDouble(sign | ((bits & 0x7fffffffffffffffL) - 1));
}
}
/**
* Get the next machine representable number after a number, moving
* in the direction of another number.
* <p>
* The ordering is as follows (increasing):
* <ul>
* <li>-INFINITY</li>
* <li>-MAX_VALUE</li>
* <li>-MIN_VALUE</li>
* <li>-0.0</li>
* <li>+0.0</li>
* <li>+MIN_VALUE</li>
* <li>+MAX_VALUE</li>
* <li>+INFINITY</li>
* <li></li>
* <p>
* If arguments compare equal, then the second argument is returned.
* <p>
* If {@code direction} is greater than {@code f},
* the smallest machine representable number strictly greater than
* {@code f} is returned; if less, then the largest representable number
* strictly less than {@code f} is returned.</p>
* <p>
* If {@code f} is infinite and direction does not
* bring it back to finite numbers, it is returned unchanged.</p>
*
* @param f base number
* @param direction (the only important thing is whether
* {@code direction} is greater or smaller than {@code f})
* @return the next machine representable number in the specified direction
*/
public static float nextAfter(final float f, final double direction) {
// handling of some
Math, 29
<FILEB>
<CHANGES>
final int n = getDimension();
for (int i = 0; i < n; i++) {
res.setEntry(i, this.getEntry(i) / v.getEntry(i));
<CHANGEE>
<CHANGES>
if (v.isNaN() || v.isInfinite()) {
final int n = getDimension();
for (int i = 0; i < n; i++) {
final double y = v.getEntry(i);
if (Double.isNaN(y)) {
res.setEntry(i, Double.NaN);
} else if (Double.isInfinite(y)) {
final double x = this.getEntry(i);
res.setEntry(i, x * y);
}
}
}
<CHANGEE>
<FILEE>
<FILEB>
@Override
public double dotProduct(RealVector v) {
if(v instanceof OpenMapRealVector) {
return dotProduct((OpenMapRealVector)v);
} else {
return super.dotProduct(v);
}
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeDivide(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
/*
* MATH-803: it is not sufficient to loop through non zero entries of
* this only. Indeed, if this[i] = 0d and v[i] = 0d, then
* this[i] / v[i] = NaN, and not 0d.
*/
<CHANGES>
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() / v.getEntry(iter.key()));
<CHANGEE>
}
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeMultiply(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() * v.getEntry(iter.key()));
}
/*
* MATH-803: the above loop assumes that 0d * x = 0d for any double x,
* which allows to consider only the non-zero entries of this. However,
* this fails if this[i] == 0d and (v[i] = NaN or v[i] = Infinity).
*
* These special cases are handled below.
*/
<CHANGES>
<CHANGEE>
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector getSubVector(int index, int n) {
checkIndex(index);
if (n < 0) {
throw new NotPositiveException(LocalizedFormats.NUMBER_OF_ELEMENTS_SHOULD_BE_POSITIVE, n);
}
checkIndex(index + n - 1);
OpenMapRealVector<SCANS> important special cases
if (Double.isNaN(f) || Double.isNaN(direction)) {
return Float.NaN;
} else if (f == direction) {
return (float) direction;
} else if (Float.isInfinite(f)) {
return (f < 0f) ? -Float.MAX_VALUE : Float.MAX_VALUE;
} else if (f == 0f) {
return (direction < 0) ? -Float.MIN_VALUE : Float.MIN_VALUE;
}
// special cases MAX_VALUE to infinity and MIN_VALUE to 0
// are handled just as normal numbers
final int bits = Float.floatToIntBits(f);
final int sign = bits & 0x80000000;
if ((direction < f) ^ (sign == 0)) {
return Float.intBitsToFloat(sign | ((bits & 0x7fffffff) + 1));
} else {
return Float.intBitsToFloat(sign | ((bits & 0x7fffffff) - 1));
}
}
/** Get the largest whole number smaller than x.
* @param x number from which floor is requested
* @return a double number f such that f is an integer f <= x < f + 1.0
*/
public static double floor(double x) {
long y;
if (x != x) { // NaN
return x;
}
if (x >= TWO_POWER_52 || x <= -TWO_POWER_52) {
return x;
}
y = (long) x;
if (x < 0 && y != x) {
y--;
}
if (y == 0) {
return x*y;
}
return y;
}
/** Get the smallest whole number larger than x.
* @param x number from which ceil is requested
* @return a double number c such that c is an integer c - 1.0 < x <= c
*/
public static double ceil(double x) {
double y;
if (x != x) { // NaN
return x;
}
y = floor(x);
if (y == x) {
return y;
}
y += 1.0;
if (
Math, 29
<FILEB>
<CHANGES>
final int n = getDimension();
for (int i = 0; i < n; i++) {
res.setEntry(i, this.getEntry(i) / v.getEntry(i));
<CHANGEE>
<CHANGES>
if (v.isNaN() || v.isInfinite()) {
final int n = getDimension();
for (int i = 0; i < n; i++) {
final double y = v.getEntry(i);
if (Double.isNaN(y)) {
res.setEntry(i, Double.NaN);
} else if (Double.isInfinite(y)) {
final double x = this.getEntry(i);
res.setEntry(i, x * y);
}
}
}
<CHANGEE>
<FILEE>
<FILEB>
@Override
public double dotProduct(RealVector v) {
if(v instanceof OpenMapRealVector) {
return dotProduct((OpenMapRealVector)v);
} else {
return super.dotProduct(v);
}
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeDivide(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
/*
* MATH-803: it is not sufficient to loop through non zero entries of
* this only. Indeed, if this[i] = 0d and v[i] = 0d, then
* this[i] / v[i] = NaN, and not 0d.
*/
<CHANGES>
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() / v.getEntry(iter.key()));
<CHANGEE>
}
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeMultiply(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() * v.getEntry(iter.key()));
}
/*
* MATH-803: the above loop assumes that 0d * x = 0d for any double x,
* which allows to consider only the non-zero entries of this. However,
* this fails if this[i] == 0d and (v[i] = NaN or v[i] = Infinity).
*
* These special cases are handled below.
*/
<CHANGES>
<CHANGEE>
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector getSubVector(int index, int n) {
checkIndex(index);
if (n < 0) {
throw new NotPositiveException(LocalizedFormats.NUMBER_OF_ELEMENTS_SHOULD_BE_POSITIVE, n);
}
checkIndex(index + n - 1);
OpenMapRealVector<SCANS>y == 0) {
return x*y;
}
return y;
}
/** Get the whole number that is the nearest to x, or the even one if x is exactly half way between two integers.
* @param x number from which nearest whole number is requested
* @return a double number r such that r is an integer r - 0.5 <= x <= r + 0.5
*/
public static double rint(double x) {
double y = floor(x);
double d = x - y;
if (d > 0.5) {
if (y == -1.0) {
return -0.0; // Preserve sign of operand
}
return y+1.0;
}
if (d < 0.5) {
return y;
}
/* half way, round to even */
long z = (long) y;
return (z & 1) == 0 ? y : y + 1.0;
}
/** Get the closest long to x.
* @param x number from which closest long is requested
* @return closest long to x
*/
public static long round(double x) {
return (long) floor(x + 0.5);
}
/** Get the closest int to x.
* @param x number from which closest int is requested
* @return closest int to x
*/
public static int round(final float x) {
return (int) floor(x + 0.5f);
}
/** Compute the minimum of two values
* @param a first value
* @param b second value
* @return a if a is lesser or equal to b, b otherwise
*/
public static int min(final int a, final int b) {
return (a <= b) ? a : b;
}
/** Compute the minimum of two values
* @param a first value
* @param b second value
* @return a if a is lesser or equal to b, b otherwise
*/
public static long min(final long a, final long b) {
return (a <= b) ? a : b;
}
/** Compute the minimum of two values
* @param a first value
* @param b second value
* @return a if a is
Math, 29
<FILEB>
<CHANGES>
final int n = getDimension();
for (int i = 0; i < n; i++) {
res.setEntry(i, this.getEntry(i) / v.getEntry(i));
<CHANGEE>
<CHANGES>
if (v.isNaN() || v.isInfinite()) {
final int n = getDimension();
for (int i = 0; i < n; i++) {
final double y = v.getEntry(i);
if (Double.isNaN(y)) {
res.setEntry(i, Double.NaN);
} else if (Double.isInfinite(y)) {
final double x = this.getEntry(i);
res.setEntry(i, x * y);
}
}
}
<CHANGEE>
<FILEE>
<FILEB>
@Override
public double dotProduct(RealVector v) {
if(v instanceof OpenMapRealVector) {
return dotProduct((OpenMapRealVector)v);
} else {
return super.dotProduct(v);
}
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeDivide(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
/*
* MATH-803: it is not sufficient to loop through non zero entries of
* this only. Indeed, if this[i] = 0d and v[i] = 0d, then
* this[i] / v[i] = NaN, and not 0d.
*/
<CHANGES>
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() / v.getEntry(iter.key()));
<CHANGEE>
}
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeMultiply(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() * v.getEntry(iter.key()));
}
/*
* MATH-803: the above loop assumes that 0d * x = 0d for any double x,
* which allows to consider only the non-zero entries of this. However,
* this fails if this[i] == 0d and (v[i] = NaN or v[i] = Infinity).
*
* These special cases are handled below.
*/
<CHANGES>
<CHANGEE>
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector getSubVector(int index, int n) {
checkIndex(index);
if (n < 0) {
throw new NotPositiveException(LocalizedFormats.NUMBER_OF_ELEMENTS_SHOULD_BE_POSITIVE, n);
}
checkIndex(index + n - 1);
OpenMapRealVector<SCANS> lesser or equal to b, b otherwise
*/
public static float min(final float a, final float b) {
if (a > b) {
return b;
}
if (a < b) {
return a;
}
/* if either arg is NaN, return NaN */
if (a != b) {
return Float.NaN;
}
/* min(+0.0,-0.0) == -0.0 */
/* 0x80000000 == Float.floatToRawIntBits(-0.0d) */
int bits = Float.floatToRawIntBits(a);
if (bits == 0x80000000) {
return a;
}
return b;
}
/** Compute the minimum of two values
* @param a first value
* @param b second value
* @return a if a is lesser or equal to b, b otherwise
*/
public static double min(final double a, final double b) {
if (a > b) {
return b;
}
if (a < b) {
return a;
}
/* if either arg is NaN, return NaN */
if (a != b) {
return Double.NaN;
}
/* min(+0.0,-0.0) == -0.0 */
/* 0x8000000000000000L == Double.doubleToRawLongBits(-0.0d) */
long bits = Double.doubleToRawLongBits(a);
if (bits == 0x8000000000000000L) {
return a;
}
return b;
}
/** Compute the maximum of two values
* @param a first value
* @param b second value
* @return b if a is lesser or equal to b, a otherwise
*/
public static int max(final int a, final int b) {
return (a <= b) ? b : a;
}
/** Compute the maximum of two values
* @param a first value
* @param b second value
* @return b if a is lesser or equal to b, a otherwise
*/
public static long max(final long
Math, 29
<FILEB>
<CHANGES>
final int n = getDimension();
for (int i = 0; i < n; i++) {
res.setEntry(i, this.getEntry(i) / v.getEntry(i));
<CHANGEE>
<CHANGES>
if (v.isNaN() || v.isInfinite()) {
final int n = getDimension();
for (int i = 0; i < n; i++) {
final double y = v.getEntry(i);
if (Double.isNaN(y)) {
res.setEntry(i, Double.NaN);
} else if (Double.isInfinite(y)) {
final double x = this.getEntry(i);
res.setEntry(i, x * y);
}
}
}
<CHANGEE>
<FILEE>
<FILEB>
@Override
public double dotProduct(RealVector v) {
if(v instanceof OpenMapRealVector) {
return dotProduct((OpenMapRealVector)v);
} else {
return super.dotProduct(v);
}
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeDivide(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
/*
* MATH-803: it is not sufficient to loop through non zero entries of
* this only. Indeed, if this[i] = 0d and v[i] = 0d, then
* this[i] / v[i] = NaN, and not 0d.
*/
<CHANGES>
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() / v.getEntry(iter.key()));
<CHANGEE>
}
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeMultiply(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() * v.getEntry(iter.key()));
}
/*
* MATH-803: the above loop assumes that 0d * x = 0d for any double x,
* which allows to consider only the non-zero entries of this. However,
* this fails if this[i] == 0d and (v[i] = NaN or v[i] = Infinity).
*
* These special cases are handled below.
*/
<CHANGES>
<CHANGEE>
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector getSubVector(int index, int n) {
checkIndex(index);
if (n < 0) {
throw new NotPositiveException(LocalizedFormats.NUMBER_OF_ELEMENTS_SHOULD_BE_POSITIVE, n);
}
checkIndex(index + n - 1);
OpenMapRealVector<SCANS> a, final long b) {
return (a <= b) ? b : a;
}
/** Compute the maximum of two values
* @param a first value
* @param b second value
* @return b if a is lesser or equal to b, a otherwise
*/
public static float max(final float a, final float b) {
if (a > b) {
return a;
}
if (a < b) {
return b;
}
/* if either arg is NaN, return NaN */
if (a != b) {
return Float.NaN;
}
/* min(+0.0,-0.0) == -0.0 */
/* 0x80000000 == Float.floatToRawIntBits(-0.0d) */
int bits = Float.floatToRawIntBits(a);
if (bits == 0x80000000) {
return b;
}
return a;
}
/** Compute the maximum of two values
* @param a first value
* @param b second value
* @return b if a is lesser or equal to b, a otherwise
*/
public static double max(final double a, final double b) {
if (a > b) {
return a;
}
if (a < b) {
return b;
}
/* if either arg is NaN, return NaN */
if (a != b) {
return Double.NaN;
}
/* min(+0.0,-0.0) == -0.0 */
/* 0x8000000000000000L == Double.doubleToRawLongBits(-0.0d) */
long bits = Double.doubleToRawLongBits(a);
if (bits == 0x8000000000000000L) {
return b;
}
return a;
}
/**
* Returns the hypotenuse of a triangle with sides {@code x} and {@code y}
* - sqrt(<i>x</i><sup>2</sup> +<i>y</i><sup>2</sup>)<br/>
* avoiding intermediate overflow or under
Math, 29
<FILEB>
<CHANGES>
final int n = getDimension();
for (int i = 0; i < n; i++) {
res.setEntry(i, this.getEntry(i) / v.getEntry(i));
<CHANGEE>
<CHANGES>
if (v.isNaN() || v.isInfinite()) {
final int n = getDimension();
for (int i = 0; i < n; i++) {
final double y = v.getEntry(i);
if (Double.isNaN(y)) {
res.setEntry(i, Double.NaN);
} else if (Double.isInfinite(y)) {
final double x = this.getEntry(i);
res.setEntry(i, x * y);
}
}
}
<CHANGEE>
<FILEE>
<FILEB>
@Override
public double dotProduct(RealVector v) {
if(v instanceof OpenMapRealVector) {
return dotProduct((OpenMapRealVector)v);
} else {
return super.dotProduct(v);
}
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeDivide(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
/*
* MATH-803: it is not sufficient to loop through non zero entries of
* this only. Indeed, if this[i] = 0d and v[i] = 0d, then
* this[i] / v[i] = NaN, and not 0d.
*/
<CHANGES>
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() / v.getEntry(iter.key()));
<CHANGEE>
}
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeMultiply(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() * v.getEntry(iter.key()));
}
/*
* MATH-803: the above loop assumes that 0d * x = 0d for any double x,
* which allows to consider only the non-zero entries of this. However,
* this fails if this[i] == 0d and (v[i] = NaN or v[i] = Infinity).
*
* These special cases are handled below.
*/
<CHANGES>
<CHANGEE>
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector getSubVector(int index, int n) {
checkIndex(index);
if (n < 0) {
throw new NotPositiveException(LocalizedFormats.NUMBER_OF_ELEMENTS_SHOULD_BE_POSITIVE, n);
}
checkIndex(index + n - 1);
OpenMapRealVector<SCANS>flow.
*
* <ul>
* <li> If either argument is infinite, then the result is positive infinity.</li>
* <li> else, if either argument is NaN then the result is NaN.</li>
* </ul>
*
* @param x a value
* @param y a value
* @return sqrt(<i>x</i><sup>2</sup> +<i>y</i><sup>2</sup>)
*/
public static double hypot(final double x, final double y) {
if (Double.isInfinite(x) || Double.isInfinite(y)) {
return Double.POSITIVE_INFINITY;
} else if (Double.isNaN(x) || Double.isNaN(y)) {
return Double.NaN;
} else {
final int expX = getExponent(x);
final int expY = getExponent(y);
if (expX > expY + 27) {
// y is neglectible with respect to x
return abs(x);
} else if (expY > expX + 27) {
// x is neglectible with respect to y
return abs(y);
} else {
// find an intermediate scale to avoid both overflow and underflow
final int middleExp = (expX + expY) / 2;
// scale parameters without losing precision
final double scaledX = scalb(x, -middleExp);
final double scaledY = scalb(y, -middleExp);
// compute scaled hypotenuse
final double scaledH = sqrt(scaledX * scaledX + scaledY * scaledY);
// remove scaling
return scalb(scaledH, middleExp);
}
}
}
/**
* Computes the remainder as prescribed by the IEEE 754 standard.
* The remainder value is mathematically equal to {@code x - y*n}
* where {@code n} is the mathematical integer closest to the exact mathematical value
* of the quotient {@code x/y}.
* If two mathematical integers are equally close to {@code x/y} then
* {@code n} is the integer that is even.
* <p>
* <ul>
* <li>If either operand is NaN, the result is NaN.</li
Math, 29
<FILEB>
<CHANGES>
final int n = getDimension();
for (int i = 0; i < n; i++) {
res.setEntry(i, this.getEntry(i) / v.getEntry(i));
<CHANGEE>
<CHANGES>
if (v.isNaN() || v.isInfinite()) {
final int n = getDimension();
for (int i = 0; i < n; i++) {
final double y = v.getEntry(i);
if (Double.isNaN(y)) {
res.setEntry(i, Double.NaN);
} else if (Double.isInfinite(y)) {
final double x = this.getEntry(i);
res.setEntry(i, x * y);
}
}
}
<CHANGEE>
<FILEE>
<FILEB>
@Override
public double dotProduct(RealVector v) {
if(v instanceof OpenMapRealVector) {
return dotProduct((OpenMapRealVector)v);
} else {
return super.dotProduct(v);
}
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeDivide(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
/*
* MATH-803: it is not sufficient to loop through non zero entries of
* this only. Indeed, if this[i] = 0d and v[i] = 0d, then
* this[i] / v[i] = NaN, and not 0d.
*/
<CHANGES>
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() / v.getEntry(iter.key()));
<CHANGEE>
}
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeMultiply(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() * v.getEntry(iter.key()));
}
/*
* MATH-803: the above loop assumes that 0d * x = 0d for any double x,
* which allows to consider only the non-zero entries of this. However,
* this fails if this[i] == 0d and (v[i] = NaN or v[i] = Infinity).
*
* These special cases are handled below.
*/
<CHANGES>
<CHANGEE>
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector getSubVector(int index, int n) {
checkIndex(index);
if (n < 0) {
throw new NotPositiveException(LocalizedFormats.NUMBER_OF_ELEMENTS_SHOULD_BE_POSITIVE, n);
}
checkIndex(index + n - 1);
OpenMapRealVector<SCANS>>
* <li>If the result is not NaN, the sign of the result equals the sign of the dividend.</li>
* <li>If the dividend is an infinity, or the divisor is a zero, or both, the result is NaN.</li>
* <li>If the dividend is finite and the divisor is an infinity, the result equals the dividend.</li>
* <li>If the dividend is a zero and the divisor is finite, the result equals the dividend.</li>
* </ul>
* <p><b>Note:</b> this implementation currently delegates to {@link StrictMath#IEEEremainder}
* @param dividend the number to be divided
* @param divisor the number by which to divide
* @return the remainder, rounded
*/
public static double IEEEremainder(double dividend, double divisor) {
return StrictMath.IEEEremainder(dividend, divisor); // TODO provide our own implementation
}
/**
* Returns the first argument with the sign of the second argument.
* A NaN {@code sign} argument is treated as positive.
*
* @param magnitude the value to return
* @param sign the sign for the returned value
* @return the magnitude with the same sign as the {@code sign} argument
*/
public static double copySign(double magnitude, double sign){
long m = Double.doubleToLongBits(magnitude);
long s = Double.doubleToLongBits(sign);
if ((m >= 0 && s >= 0) || (m < 0 && s < 0)) { // Sign is currently OK
return magnitude;
}
return -magnitude; // flip sign
}
/**
* Returns the first argument with the sign of the second argument.
* A NaN {@code sign} argument is treated as positive.
*
* @param magnitude the value to return
* @param sign the sign for the returned value
* @return the magnitude with the same sign as the {@code sign} argument
*/
public static float copySign(float magnitude, float sign){
int m = Float.floatToIntBits(magnitude);
int s = Float.floatToIntBits(sign);
if ((m >= 0 && s >= 0) || (m < 0 && s < 0)) {
Math, 29
<FILEB>
<CHANGES>
final int n = getDimension();
for (int i = 0; i < n; i++) {
res.setEntry(i, this.getEntry(i) / v.getEntry(i));
<CHANGEE>
<CHANGES>
if (v.isNaN() || v.isInfinite()) {
final int n = getDimension();
for (int i = 0; i < n; i++) {
final double y = v.getEntry(i);
if (Double.isNaN(y)) {
res.setEntry(i, Double.NaN);
} else if (Double.isInfinite(y)) {
final double x = this.getEntry(i);
res.setEntry(i, x * y);
}
}
}
<CHANGEE>
<FILEE>
<FILEB>
@Override
public double dotProduct(RealVector v) {
if(v instanceof OpenMapRealVector) {
return dotProduct((OpenMapRealVector)v);
} else {
return super.dotProduct(v);
}
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeDivide(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
/*
* MATH-803: it is not sufficient to loop through non zero entries of
* this only. Indeed, if this[i] = 0d and v[i] = 0d, then
* this[i] / v[i] = NaN, and not 0d.
*/
<CHANGES>
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() / v.getEntry(iter.key()));
<CHANGEE>
}
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeMultiply(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() * v.getEntry(iter.key()));
}
/*
* MATH-803: the above loop assumes that 0d * x = 0d for any double x,
* which allows to consider only the non-zero entries of this. However,
* this fails if this[i] == 0d and (v[i] = NaN or v[i] = Infinity).
*
* These special cases are handled below.
*/
<CHANGES>
<CHANGEE>
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector getSubVector(int index, int n) {
checkIndex(index);
if (n < 0) {
throw new NotPositiveException(LocalizedFormats.NUMBER_OF_ELEMENTS_SHOULD_BE_POSITIVE, n);
}
checkIndex(index + n - 1);
OpenMapRealVector<SCANS> // Sign is currently OK
return magnitude;
}
return -magnitude; // flip sign
}
/**
* Return the exponent of a double number, removing the bias.
* <p>
* For double numbers of the form 2<sup>x</sup>, the unbiased
* exponent is exactly x.
* </p>
* @param d number from which exponent is requested
* @return exponent for d in IEEE754 representation, without bias
*/
public static int getExponent(final double d) {
return (int) ((Double.doubleToLongBits(d) >>> 52) & 0x7ff) - 1023;
}
/**
* Return the exponent of a float number, removing the bias.
* <p>
* For float numbers of the form 2<sup>x</sup>, the unbiased
* exponent is exactly x.
* </p>
* @param f number from which exponent is requested
* @return exponent for d in IEEE754 representation, without bias
*/
public static int getExponent(final float f) {
return ((Float.floatToIntBits(f) >>> 23) & 0xff) - 127;
}
/**
* Print out contents of arrays, and check the length.
* <p>used to generate the preset arrays originally.</p>
* @param a unused
*/
public static void main(String[] a) {
PrintStream out = System.out;
FastMathCalc.printarray(out, "EXP_INT_TABLE_A", EXP_INT_TABLE_LEN, ExpIntTable.EXP_INT_TABLE_A);
FastMathCalc.printarray(out, "EXP_INT_TABLE_B", EXP_INT_TABLE_LEN, ExpIntTable.EXP_INT_TABLE_B);
FastMathCalc.printarray(out, "EXP_FRAC_TABLE_A", EXP_FRAC_TABLE_LEN, ExpFracTable.EXP_FRAC_TABLE_A);
FastMathCalc.printarray(out, "EXP_FRAC_TABLE_B", EXP_FRAC_TABLE_LEN, ExpFracTable.EXP_FRAC_TABLE_B);
FastMathCalc.printarray(out, "LN_
Math, 29
<FILEB>
<CHANGES>
final int n = getDimension();
for (int i = 0; i < n; i++) {
res.setEntry(i, this.getEntry(i) / v.getEntry(i));
<CHANGEE>
<CHANGES>
if (v.isNaN() || v.isInfinite()) {
final int n = getDimension();
for (int i = 0; i < n; i++) {
final double y = v.getEntry(i);
if (Double.isNaN(y)) {
res.setEntry(i, Double.NaN);
} else if (Double.isInfinite(y)) {
final double x = this.getEntry(i);
res.setEntry(i, x * y);
}
}
}
<CHANGEE>
<FILEE>
<FILEB>
@Override
public double dotProduct(RealVector v) {
if(v instanceof OpenMapRealVector) {
return dotProduct((OpenMapRealVector)v);
} else {
return super.dotProduct(v);
}
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeDivide(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
/*
* MATH-803: it is not sufficient to loop through non zero entries of
* this only. Indeed, if this[i] = 0d and v[i] = 0d, then
* this[i] / v[i] = NaN, and not 0d.
*/
<CHANGES>
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() / v.getEntry(iter.key()));
<CHANGEE>
}
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeMultiply(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() * v.getEntry(iter.key()));
}
/*
* MATH-803: the above loop assumes that 0d * x = 0d for any double x,
* which allows to consider only the non-zero entries of this. However,
* this fails if this[i] == 0d and (v[i] = NaN or v[i] = Infinity).
*
* These special cases are handled below.
*/
<CHANGES>
<CHANGEE>
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector getSubVector(int index, int n) {
checkIndex(index);
if (n < 0) {
throw new NotPositiveException(LocalizedFormats.NUMBER_OF_ELEMENTS_SHOULD_BE_POSITIVE, n);
}
checkIndex(index + n - 1);
OpenMapRealVector<SCANS>MANT",LN_MANT_LEN, lnMant.LN_MANT);
FastMathCalc.printarray(out, "SINE_TABLE_A", SINE_TABLE_LEN, SINE_TABLE_A);
FastMathCalc.printarray(out, "SINE_TABLE_B", SINE_TABLE_LEN, SINE_TABLE_B);
FastMathCalc.printarray(out, "COSINE_TABLE_A", SINE_TABLE_LEN, COSINE_TABLE_A);
FastMathCalc.printarray(out, "COSINE_TABLE_B", SINE_TABLE_LEN, COSINE_TABLE_B);
FastMathCalc.printarray(out, "TANGENT_TABLE_A", SINE_TABLE_LEN, TANGENT_TABLE_A);
FastMathCalc.printarray(out, "TANGENT_TABLE_B", SINE_TABLE_LEN, TANGENT_TABLE_B);
}
/** Enclose large data table in nested static class so it's only loaded on first access. */
private static class ExpIntTable {
/** Exponential evaluated at integer values,
* exp(x) = expIntTableA[x + EXP_INT_TABLE_MAX_INDEX] + expIntTableB[x+EXP_INT_TABLE_MAX_INDEX].
*/
private static final double[] EXP_INT_TABLE_A;
/** Exponential evaluated at integer values,
* exp(x) = expIntTableA[x + EXP_INT_TABLE_MAX_INDEX] + expIntTableB[x+EXP_INT_TABLE_MAX_INDEX]
*/
private static final double[] EXP_INT_TABLE_B;
static {
if (RECOMPUTE_TABLES_AT_RUNTIME) {
EXP_INT_TABLE_A = new double[FastMath.EXP_INT_TABLE_LEN];
EXP_INT_TABLE_B = new double[FastMath.EXP_INT_TABLE_LEN];
final double tmp[] = new double[2];
final double recip[] = new double[2];
// Populate expIntTable
for (int i = 0; i < FastMath.EXP_INT_TABLE_MAX_INDEX; i++) {
FastMathCalc.exp
Math, 29
<FILEB>
<CHANGES>
final int n = getDimension();
for (int i = 0; i < n; i++) {
res.setEntry(i, this.getEntry(i) / v.getEntry(i));
<CHANGEE>
<CHANGES>
if (v.isNaN() || v.isInfinite()) {
final int n = getDimension();
for (int i = 0; i < n; i++) {
final double y = v.getEntry(i);
if (Double.isNaN(y)) {
res.setEntry(i, Double.NaN);
} else if (Double.isInfinite(y)) {
final double x = this.getEntry(i);
res.setEntry(i, x * y);
}
}
}
<CHANGEE>
<FILEE>
<FILEB>
@Override
public double dotProduct(RealVector v) {
if(v instanceof OpenMapRealVector) {
return dotProduct((OpenMapRealVector)v);
} else {
return super.dotProduct(v);
}
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeDivide(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
/*
* MATH-803: it is not sufficient to loop through non zero entries of
* this only. Indeed, if this[i] = 0d and v[i] = 0d, then
* this[i] / v[i] = NaN, and not 0d.
*/
<CHANGES>
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() / v.getEntry(iter.key()));
<CHANGEE>
}
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeMultiply(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() * v.getEntry(iter.key()));
}
/*
* MATH-803: the above loop assumes that 0d * x = 0d for any double x,
* which allows to consider only the non-zero entries of this. However,
* this fails if this[i] == 0d and (v[i] = NaN or v[i] = Infinity).
*
* These special cases are handled below.
*/
<CHANGES>
<CHANGEE>
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector getSubVector(int index, int n) {
checkIndex(index);
if (n < 0) {
throw new NotPositiveException(LocalizedFormats.NUMBER_OF_ELEMENTS_SHOULD_BE_POSITIVE, n);
}
checkIndex(index + n - 1);
OpenMapRealVector<SCANS>int(i, tmp);
EXP_INT_TABLE_A[i + FastMath.EXP_INT_TABLE_MAX_INDEX] = tmp[0];
EXP_INT_TABLE_B[i + FastMath.EXP_INT_TABLE_MAX_INDEX] = tmp[1];
if (i != 0) {
// Negative integer powers
FastMathCalc.splitReciprocal(tmp, recip);
EXP_INT_TABLE_A[FastMath.EXP_INT_TABLE_MAX_INDEX - i] = recip[0];
EXP_INT_TABLE_B[FastMath.EXP_INT_TABLE_MAX_INDEX - i] = recip[1];
}
}
} else {
EXP_INT_TABLE_A = FastMathLiteralArrays.loadExpIntA();
EXP_INT_TABLE_B = FastMathLiteralArrays.loadExpIntB();
}
}
}
/** Enclose large data table in nested static class so it's only loaded on first access. */
private static class ExpFracTable {
/** Exponential over the range of 0 - 1 in increments of 2^-10
* exp(x/1024) = expFracTableA[x] + expFracTableB[x].
* 1024 = 2^10
*/
private static final double[] EXP_FRAC_TABLE_A;
/** Exponential over the range of 0 - 1 in increments of 2^-10
* exp(x/1024) = expFracTableA[x] + expFracTableB[x].
*/
private static final double[] EXP_FRAC_TABLE_B;
static {
if (RECOMPUTE_TABLES_AT_RUNTIME) {
EXP_FRAC_TABLE_A = new double[FastMath.EXP_FRAC_TABLE_LEN];
EXP_FRAC_TABLE_B = new double[FastMath.EXP_FRAC_TABLE_LEN];
final double tmp[] = new double[2];
// Populate expFracTable
final double factor = 1d / (EXP_FRAC_TABLE_LEN - 1);
for (int i = 0; i < EXP_FRAC_TABLE
Math, 29
<FILEB>
<CHANGES>
final int n = getDimension();
for (int i = 0; i < n; i++) {
res.setEntry(i, this.getEntry(i) / v.getEntry(i));
<CHANGEE>
<CHANGES>
if (v.isNaN() || v.isInfinite()) {
final int n = getDimension();
for (int i = 0; i < n; i++) {
final double y = v.getEntry(i);
if (Double.isNaN(y)) {
res.setEntry(i, Double.NaN);
} else if (Double.isInfinite(y)) {
final double x = this.getEntry(i);
res.setEntry(i, x * y);
}
}
}
<CHANGEE>
<FILEE>
<FILEB>
@Override
public double dotProduct(RealVector v) {
if(v instanceof OpenMapRealVector) {
return dotProduct((OpenMapRealVector)v);
} else {
return super.dotProduct(v);
}
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeDivide(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
/*
* MATH-803: it is not sufficient to loop through non zero entries of
* this only. Indeed, if this[i] = 0d and v[i] = 0d, then
* this[i] / v[i] = NaN, and not 0d.
*/
<CHANGES>
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() / v.getEntry(iter.key()));
<CHANGEE>
}
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeMultiply(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() * v.getEntry(iter.key()));
}
/*
* MATH-803: the above loop assumes that 0d * x = 0d for any double x,
* which allows to consider only the non-zero entries of this. However,
* this fails if this[i] == 0d and (v[i] = NaN or v[i] = Infinity).
*
* These special cases are handled below.
*/
<CHANGES>
<CHANGEE>
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector getSubVector(int index, int n) {
checkIndex(index);
if (n < 0) {
throw new NotPositiveException(LocalizedFormats.NUMBER_OF_ELEMENTS_SHOULD_BE_POSITIVE, n);
}
checkIndex(index + n - 1);
OpenMapRealVector<SCANS>_A.length; i++) {
FastMathCalc.slowexp(i * factor, tmp);
EXP_FRAC_TABLE_A[i] = tmp[0];
EXP_FRAC_TABLE_B[i] = tmp[1];
}
} else {
EXP_FRAC_TABLE_A = FastMathLiteralArrays.loadExpFracA();
EXP_FRAC_TABLE_B = FastMathLiteralArrays.loadExpFracB();
}
}
}
/** Enclose large data table in nested static class so it's only loaded on first access. */
private static class lnMant {
/** Extended precision logarithm table over the range 1 - 2 in increments of 2^-10. */
private static final double[][] LN_MANT;
static {
if (RECOMPUTE_TABLES_AT_RUNTIME) {
LN_MANT = new double[FastMath.LN_MANT_LEN][];
// Populate lnMant table
for (int i = 0; i < LN_MANT.length; i++) {
final double d = Double.longBitsToDouble( (((long) i) << 42) | 0x3ff0000000000000L );
LN_MANT[i] = FastMathCalc.slowLog(d);
}
} else {
LN_MANT = FastMathLiteralArrays.loadLnMant();
}
}
}
}
Math, 29
<FILEB>
<CHANGES>
final int n = getDimension();
for (int i = 0; i < n; i++) {
res.setEntry(i, this.getEntry(i) / v.getEntry(i));
<CHANGEE>
<CHANGES>
if (v.isNaN() || v.isInfinite()) {
final int n = getDimension();
for (int i = 0; i < n; i++) {
final double y = v.getEntry(i);
if (Double.isNaN(y)) {
res.setEntry(i, Double.NaN);
} else if (Double.isInfinite(y)) {
final double x = this.getEntry(i);
res.setEntry(i, x * y);
}
}
}
<CHANGEE>
<FILEE>
<FILEB>
@Override
public double dotProduct(RealVector v) {
if(v instanceof OpenMapRealVector) {
return dotProduct((OpenMapRealVector)v);
} else {
return super.dotProduct(v);
}
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeDivide(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
/*
* MATH-803: it is not sufficient to loop through non zero entries of
* this only. Indeed, if this[i] = 0d and v[i] = 0d, then
* this[i] / v[i] = NaN, and not 0d.
*/
<CHANGES>
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() / v.getEntry(iter.key()));
<CHANGEE>
}
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeMultiply(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() * v.getEntry(iter.key()));
}
/*
* MATH-803: the above loop assumes that 0d * x = 0d for any double x,
* which allows to consider only the non-zero entries of this. However,
* this fails if this[i] == 0d and (v[i] = NaN or v[i] = Infinity).
*
* These special cases are handled below.
*/
<CHANGES>
<CHANGEE>
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector getSubVector(int index, int n) {
checkIndex(index);
if (n < 0) {
throw new NotPositiveException(LocalizedFormats.NUMBER_OF_ELEMENTS_SHOULD_BE_POSITIVE, n);
}
checkIndex(index + n - 1);
OpenMapRealVector<SCANS>
@Override
public RealVector subtract(RealVector v) {
throw unsupported();
}
@Override
public RealVector mapAdd(double d) {
throw unsupported();
}
@Override
public RealVector mapAddToSelf(double d) {
throw unsupported();
}
@Override
public RealVector mapSubtract(double d) {
throw unsupported();
}
@Override
public RealVector mapSubtractToSelf(double d) {
throw unsupported();
}
@Override
public RealVector mapMultiply(double d) {
double[] out = new double[data.length];
for (int i = 0; i < data.length; i++) {
out[i] = data[i] * d;
}
return new OpenMapRealVector(out);
}
@Override
public RealVector mapMultiplyToSelf(double d) {
throw unsupported();
}
@Override
public RealVector mapDivide(double d) {
throw unsupported();
}
@Override
public RealVector mapDivideToSelf(double d) {
throw unsupported();
}
@Override
public RealVector ebeMultiply(RealVector v) {
throw unsupported();
}
@Override
public RealVector ebeDivide(RealVector v) {
throw unsupported();
}
@Override
public double dotProduct(RealVector v) {
double dot = 0;
for (int i = 0; i < data.length; i++) {
dot += data[i] * v.getEntry(i);
}
return dot;
}
@Override
public double getNorm() {
throw unsupported();
}
@Override
public double getL1Norm() {
throw unsupported();
}
@Override
public double getLInfNorm() {
throw unsupported();
}
@Override
public double getDistance(RealVector v) {
throw unsupported();
}
@Override
public double getL1Distance(RealVector v) {
throw unsupported();
}
@Override
public double getLInfDistance(RealVector v) {
throw unsupported();
}
@Override
public RealVector unitVector() {
throw unsupported();
}
@Override
public void unitize() {
throw unsupported();
}
@Override
public RealVector projection(RealVector v) {
throw unsupported
Math, 29
<FILEB>
<CHANGES>
final int n = getDimension();
for (int i = 0; i < n; i++) {
res.setEntry(i, this.getEntry(i) / v.getEntry(i));
<CHANGEE>
<CHANGES>
if (v.isNaN() || v.isInfinite()) {
final int n = getDimension();
for (int i = 0; i < n; i++) {
final double y = v.getEntry(i);
if (Double.isNaN(y)) {
res.setEntry(i, Double.NaN);
} else if (Double.isInfinite(y)) {
final double x = this.getEntry(i);
res.setEntry(i, x * y);
}
}
}
<CHANGEE>
<FILEE>
<FILEB>
@Override
public double dotProduct(RealVector v) {
if(v instanceof OpenMapRealVector) {
return dotProduct((OpenMapRealVector)v);
} else {
return super.dotProduct(v);
}
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeDivide(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
/*
* MATH-803: it is not sufficient to loop through non zero entries of
* this only. Indeed, if this[i] = 0d and v[i] = 0d, then
* this[i] / v[i] = NaN, and not 0d.
*/
<CHANGES>
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() / v.getEntry(iter.key()));
<CHANGEE>
}
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeMultiply(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() * v.getEntry(iter.key()));
}
/*
* MATH-803: the above loop assumes that 0d * x = 0d for any double x,
* which allows to consider only the non-zero entries of this. However,
* this fails if this[i] == 0d and (v[i] = NaN or v[i] = Infinity).
*
* These special cases are handled below.
*/
<CHANGES>
<CHANGEE>
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector getSubVector(int index, int n) {
checkIndex(index);
if (n < 0) {
throw new NotPositiveException(LocalizedFormats.NUMBER_OF_ELEMENTS_SHOULD_BE_POSITIVE, n);
}
checkIndex(index + n - 1);
OpenMapRealVector<SCANS>();
}
@Override
public RealMatrix outerProduct(RealVector v) {
throw unsupported();
}
@Override
public double getEntry(int index) {
return data[index];
}
@Override
public int getDimension() {
return data.length;
}
@Override
public RealVector append(RealVector v) {
throw unsupported();
}
@Override
public RealVector append(double d) {
throw unsupported();
}
@Override
public RealVector getSubVector(int index, int n) {
throw unsupported();
}
@Override
public void setEntry(int index, double value) {
data[index] = value;
}
@Override
public void setSubVector(int index, RealVector v) {
throw unsupported();
}
@Override
public void set(double value) {
throw unsupported();
}
@Override
public double[] toArray() {
return data.clone();
}
@Override
public boolean isNaN() {
boolean isNaN = false;
for (int i = 0; i < data.length; i++) {
isNaN |= Double.isNaN(data[i]);
}
return isNaN;
}
@Override
public boolean isInfinite() {
boolean isInfinite = false;
for (int i = 0; i < data.length; i++) {
final double x = data[i];
if (Double.isNaN(x)) {
return false;
}
isInfinite |= Double.isInfinite(x);
}
return isInfinite;
}
}
@Override
public RealVector create(double[] data) {
return new OpenMapRealVector(data);
}
@Override
public RealVector createAlien(double[] data) {
return new SparseRealVectorTestImpl(data);
}
@Test
public void testConstructors() {
OpenMapRealVector v0 = new OpenMapRealVector();
Assert.assertEquals("testData len", 0, v0.getDimension());
OpenMapRealVector v1 = new OpenMapRealVector(7);
Assert.assertEquals("testData len", 7, v1.getDimension());
Assert.assertEquals("testData is 0.0 ", 0.0, v1.getEntry(6), 0);
Math, 29
<FILEB>
<CHANGES>
final int n = getDimension();
for (int i = 0; i < n; i++) {
res.setEntry(i, this.getEntry(i) / v.getEntry(i));
<CHANGEE>
<CHANGES>
if (v.isNaN() || v.isInfinite()) {
final int n = getDimension();
for (int i = 0; i < n; i++) {
final double y = v.getEntry(i);
if (Double.isNaN(y)) {
res.setEntry(i, Double.NaN);
} else if (Double.isInfinite(y)) {
final double x = this.getEntry(i);
res.setEntry(i, x * y);
}
}
}
<CHANGEE>
<FILEE>
<FILEB>
@Override
public double dotProduct(RealVector v) {
if(v instanceof OpenMapRealVector) {
return dotProduct((OpenMapRealVector)v);
} else {
return super.dotProduct(v);
}
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeDivide(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
/*
* MATH-803: it is not sufficient to loop through non zero entries of
* this only. Indeed, if this[i] = 0d and v[i] = 0d, then
* this[i] / v[i] = NaN, and not 0d.
*/
<CHANGES>
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() / v.getEntry(iter.key()));
<CHANGEE>
}
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeMultiply(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() * v.getEntry(iter.key()));
}
/*
* MATH-803: the above loop assumes that 0d * x = 0d for any double x,
* which allows to consider only the non-zero entries of this. However,
* this fails if this[i] == 0d and (v[i] = NaN or v[i] = Infinity).
*
* These special cases are handled below.
*/
<CHANGES>
<CHANGEE>
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector getSubVector(int index, int n) {
checkIndex(index);
if (n < 0) {
throw new NotPositiveException(LocalizedFormats.NUMBER_OF_ELEMENTS_SHOULD_BE_POSITIVE, n);
}
checkIndex(index + n - 1);
OpenMapRealVector<SCANS> made.
*
* @param data the entries of the vector to be created
* @return a new {@link RealVector} of an alien type
*/
public abstract RealVector createAlien(double[] data);
/**
* Returns a preferred value of the entries, to be tested specifically. Some
* implementations of {@link RealVector} (e.g. {@link OpenMapRealVector}) do
* not store specific values of entries. In order to ensure that all tests
* take into account this specific value, some entries of the vectors to be
* tested are deliberately set to the value returned by the present method.
* The default implementation returns {@code 0.0}.
*
* @return a value which <em>should</em> be present in all vectors to be
* tested
*/
public double getPreferredEntryValue() {
return 0.0;
}
/** verifies that two vectors are close (sup norm) */
protected void assertClose(String msg, double[] m, double[] n,
double tolerance) {
if (m.length != n.length) {
Assert.fail("vectors have different lengths");
}
for (int i = 0; i < m.length; i++) {
Assert.assertEquals(msg + " " + i + " elements differ", m[i],n[i],tolerance);
}
}
protected double[][] ma1 = {{1d, 2d, 3d}, {4d, 5d, 6d}, {7d, 8d, 9d}};
protected double[] vec1 = {1d, 2d, 3d};
protected double[] vec2 = {4d, 5d, 6d};
protected double[] vec3 = {7d, 8d, 9d};
protected double[] vec4 = {1d, 2d, 3d, 4d, 5d, 6d, 7d, 8d, 9d};
protected double[] vec5 = { -4d, 0d, 3d, 1d, -6d, 3d};
protected double[] vec_null = {0d, 0d, 0d};
protected Double[] dvec1 = {1
Math, 29
<FILEB>
<CHANGES>
final int n = getDimension();
for (int i = 0; i < n; i++) {
res.setEntry(i, this.getEntry(i) / v.getEntry(i));
<CHANGEE>
<CHANGES>
if (v.isNaN() || v.isInfinite()) {
final int n = getDimension();
for (int i = 0; i < n; i++) {
final double y = v.getEntry(i);
if (Double.isNaN(y)) {
res.setEntry(i, Double.NaN);
} else if (Double.isInfinite(y)) {
final double x = this.getEntry(i);
res.setEntry(i, x * y);
}
}
}
<CHANGEE>
<FILEE>
<FILEB>
@Override
public double dotProduct(RealVector v) {
if(v instanceof OpenMapRealVector) {
return dotProduct((OpenMapRealVector)v);
} else {
return super.dotProduct(v);
}
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeDivide(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
/*
* MATH-803: it is not sufficient to loop through non zero entries of
* this only. Indeed, if this[i] = 0d and v[i] = 0d, then
* this[i] / v[i] = NaN, and not 0d.
*/
<CHANGES>
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() / v.getEntry(iter.key()));
<CHANGEE>
}
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeMultiply(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() * v.getEntry(iter.key()));
}
/*
* MATH-803: the above loop assumes that 0d * x = 0d for any double x,
* which allows to consider only the non-zero entries of this. However,
* this fails if this[i] == 0d and (v[i] = NaN or v[i] = Infinity).
*
* These special cases are handled below.
*/
<CHANGES>
<CHANGEE>
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector getSubVector(int index, int n) {
checkIndex(index);
if (n < 0) {
throw new NotPositiveException(LocalizedFormats.NUMBER_OF_ELEMENTS_SHOULD_BE_POSITIVE, n);
}
checkIndex(index + n - 1);
OpenMapRealVector<SCANS>d, 2d, 3d, 4d, 5d, 6d, 7d, 8d, 9d};
protected double[][] mat1 = {{1d, 2d, 3d}, {4d, 5d, 6d},{ 7d, 8d, 9d}};
/**
* Data which can be used to create a specific vector. The array is
* interspersed with the value returned by
* {@link #getPreferredEntryValue()}.
*/
private final double[] data1;
/**
* Data which can be used to create a specific vector. The array is
* interspersed with the value returned by
* {@link #getPreferredEntryValue()}.
*/
private final double[] data2;
public RealVectorAbstractTest() {
final double x = getPreferredEntryValue();
data1 = new double[] {x, 1d, 2d, x, x};
data2 = new double[] {x, x, 3d, x, 4d, x};
}
// tolerances
protected double entryTolerance = 10E-16;
protected double normTolerance = 10E-14;
@Test
public void testGetDimension() {
Assert.assertEquals(data1.length, create(data1).getDimension());
}
@Test
public void testGetEntry() {
final RealVector v = create(data1);
for (int i = 0; i < data1.length; i++) {
Assert.assertEquals("entry " + i, data1[i], v.getEntry(i), 0d);
}
}
@Test(expected=OutOfRangeException.class)
public void testGetEntryInvalidIndex1() {
create(data1).getEntry(-1);
}
@Test(expected=OutOfRangeException.class)
public void testGetEntryInvalidIndex2() {
create(data1).getEntry(data1.length);
}
@Test
public void testSetEntry() {
final double[] expected = MathArrays.copyOf(data1);
final RealVector actual = create(data1);
/*
* Try setting to any value.
*/
for (int i = 0; i < data1.length; i++) {
final double oldValue
Math, 29
<FILEB>
<CHANGES>
final int n = getDimension();
for (int i = 0; i < n; i++) {
res.setEntry(i, this.getEntry(i) / v.getEntry(i));
<CHANGEE>
<CHANGES>
if (v.isNaN() || v.isInfinite()) {
final int n = getDimension();
for (int i = 0; i < n; i++) {
final double y = v.getEntry(i);
if (Double.isNaN(y)) {
res.setEntry(i, Double.NaN);
} else if (Double.isInfinite(y)) {
final double x = this.getEntry(i);
res.setEntry(i, x * y);
}
}
}
<CHANGEE>
<FILEE>
<FILEB>
@Override
public double dotProduct(RealVector v) {
if(v instanceof OpenMapRealVector) {
return dotProduct((OpenMapRealVector)v);
} else {
return super.dotProduct(v);
}
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeDivide(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
/*
* MATH-803: it is not sufficient to loop through non zero entries of
* this only. Indeed, if this[i] = 0d and v[i] = 0d, then
* this[i] / v[i] = NaN, and not 0d.
*/
<CHANGES>
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() / v.getEntry(iter.key()));
<CHANGEE>
}
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeMultiply(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() * v.getEntry(iter.key()));
}
/*
* MATH-803: the above loop assumes that 0d * x = 0d for any double x,
* which allows to consider only the non-zero entries of this. However,
* this fails if this[i] == 0d and (v[i] = NaN or v[i] = Infinity).
*
* These special cases are handled below.
*/
<CHANGES>
<CHANGEE>
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector getSubVector(int index, int n) {
checkIndex(index);
if (n < 0) {
throw new NotPositiveException(LocalizedFormats.NUMBER_OF_ELEMENTS_SHOULD_BE_POSITIVE, n);
}
checkIndex(index + n - 1);
OpenMapRealVector<SCANS> = data1[i];
final double newValue = oldValue + 1d;
expected[i] = newValue;
actual.setEntry(i, newValue);
TestUtils.assertEquals("while setting entry #" + i, expected,
actual, 0d);
expected[i] = oldValue;
actual.setEntry(i, oldValue);
}
/*
* Try setting to the preferred value.
*/
final double x = getPreferredEntryValue();
for (int i = 0; i < data1.length; i++) {
final double oldValue = data1[i];
final double newValue = x;
expected[i] = newValue;
actual.setEntry(i, newValue);
TestUtils.assertEquals("while setting entry #" + i, expected,
actual, 0d);
expected[i] = oldValue;
actual.setEntry(i, oldValue);
}
}
@Test(expected=OutOfRangeException.class)
public void testSetEntryInvalidIndex1() {
create(data1).setEntry(-1, getPreferredEntryValue());
}
@Test(expected=OutOfRangeException.class)
public void testSetEntryInvalidIndex2() {
create(data1).setEntry(data1.length, getPreferredEntryValue());
}
@Test
public void testAddToEntry() {
final double[] expected = MathArrays.copyOf(data1);
final RealVector actual = create(data1);
/*
* Try adding any value.
*/
double increment = 1d;
for (int i = 0; i < data1.length; i++) {
final double oldValue = data1[i];
expected[i] += increment;
actual.addToEntry(i, increment);
TestUtils.assertEquals("while incrementing entry #" + i, expected,
actual, 0d);
expected[i] = oldValue;
actual.setEntry(i, oldValue);
}
/*
* Try incrementing so that result is equal to preferred value.
*/
final double x = getPreferredEntryValue();
for (int i = 0; i < data1.length; i++) {
final double oldValue = data1[i];
increment = x - oldValue;
expected[i] = x;
actual.addToEntry(i, increment);
TestUtils.
Math, 29
<FILEB>
<CHANGES>
final int n = getDimension();
for (int i = 0; i < n; i++) {
res.setEntry(i, this.getEntry(i) / v.getEntry(i));
<CHANGEE>
<CHANGES>
if (v.isNaN() || v.isInfinite()) {
final int n = getDimension();
for (int i = 0; i < n; i++) {
final double y = v.getEntry(i);
if (Double.isNaN(y)) {
res.setEntry(i, Double.NaN);
} else if (Double.isInfinite(y)) {
final double x = this.getEntry(i);
res.setEntry(i, x * y);
}
}
}
<CHANGEE>
<FILEE>
<FILEB>
@Override
public double dotProduct(RealVector v) {
if(v instanceof OpenMapRealVector) {
return dotProduct((OpenMapRealVector)v);
} else {
return super.dotProduct(v);
}
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeDivide(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
/*
* MATH-803: it is not sufficient to loop through non zero entries of
* this only. Indeed, if this[i] = 0d and v[i] = 0d, then
* this[i] / v[i] = NaN, and not 0d.
*/
<CHANGES>
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() / v.getEntry(iter.key()));
<CHANGEE>
}
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeMultiply(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() * v.getEntry(iter.key()));
}
/*
* MATH-803: the above loop assumes that 0d * x = 0d for any double x,
* which allows to consider only the non-zero entries of this. However,
* this fails if this[i] == 0d and (v[i] = NaN or v[i] = Infinity).
*
* These special cases are handled below.
*/
<CHANGES>
<CHANGEE>
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector getSubVector(int index, int n) {
checkIndex(index);
if (n < 0) {
throw new NotPositiveException(LocalizedFormats.NUMBER_OF_ELEMENTS_SHOULD_BE_POSITIVE, n);
}
checkIndex(index + n - 1);
OpenMapRealVector<SCANS>assertEquals("while incrementing entry #" + i, expected,
actual, 0d);
expected[i] = oldValue;
actual.setEntry(i, oldValue);
}
}
@Test(expected=OutOfRangeException.class)
public void testAddToEntryInvalidIndex1() {
create(data1).addToEntry(-1, getPreferredEntryValue());
}
@Test(expected=OutOfRangeException.class)
public void testAddToEntryInvalidIndex2() {
create(data1).addToEntry(data1.length, getPreferredEntryValue());
}
private void doTestAppendVector(final String message, final RealVector v1,
final RealVector v2, final double delta) {
final int n1 = v1.getDimension();
final int n2 = v2.getDimension();
final RealVector v = v1.append(v2);
Assert.assertEquals(message, n1 + n2, v.getDimension());
for (int i = 0; i < n1; i++) {
final String msg = message + ", entry #" + i;
Assert.assertEquals(msg, v1.getEntry(i), v.getEntry(i), delta);
}
for (int i = 0; i < n2; i++) {
final String msg = message + ", entry #" + (n1 + i);
Assert.assertEquals(msg, v2.getEntry(i), v.getEntry(n1 + i), delta);
}
}
@Test
public void testAppendVector() {
doTestAppendVector("same type", create(data1), create(data2), 0d);
doTestAppendVector("mixed types", create(data1), createAlien(data2), 0d);
}
private void doTestAppendScalar(final String message, final RealVector v,
final double d, final double delta) {
final int n = v.getDimension();
final RealVector w = v.append(d);
Assert.assertEquals(message, n + 1, w.getDimension());
for (int i = 0; i < n; i++) {
final String msg = message + ", entry #" + i;
Assert.assertEquals(msg, v.getEntry(i), w.getEntry(i), delta);
}
Math, 29
<FILEB>
<CHANGES>
final int n = getDimension();
for (int i = 0; i < n; i++) {
res.setEntry(i, this.getEntry(i) / v.getEntry(i));
<CHANGEE>
<CHANGES>
if (v.isNaN() || v.isInfinite()) {
final int n = getDimension();
for (int i = 0; i < n; i++) {
final double y = v.getEntry(i);
if (Double.isNaN(y)) {
res.setEntry(i, Double.NaN);
} else if (Double.isInfinite(y)) {
final double x = this.getEntry(i);
res.setEntry(i, x * y);
}
}
}
<CHANGEE>
<FILEE>
<FILEB>
@Override
public double dotProduct(RealVector v) {
if(v instanceof OpenMapRealVector) {
return dotProduct((OpenMapRealVector)v);
} else {
return super.dotProduct(v);
}
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeDivide(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
/*
* MATH-803: it is not sufficient to loop through non zero entries of
* this only. Indeed, if this[i] = 0d and v[i] = 0d, then
* this[i] / v[i] = NaN, and not 0d.
*/
<CHANGES>
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() / v.getEntry(iter.key()));
<CHANGEE>
}
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeMultiply(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() * v.getEntry(iter.key()));
}
/*
* MATH-803: the above loop assumes that 0d * x = 0d for any double x,
* which allows to consider only the non-zero entries of this. However,
* this fails if this[i] == 0d and (v[i] = NaN or v[i] = Infinity).
*
* These special cases are handled below.
*/
<CHANGES>
<CHANGEE>
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector getSubVector(int index, int n) {
checkIndex(index);
if (n < 0) {
throw new NotPositiveException(LocalizedFormats.NUMBER_OF_ELEMENTS_SHOULD_BE_POSITIVE, n);
}
checkIndex(index + n - 1);
OpenMapRealVector<SCANS> final String msg = message + ", entry #" + n;
Assert.assertEquals(msg, d, w.getEntry(n), delta);
}
@Test
public void testAppendScalar() {
doTestAppendScalar("", create(data1), 1d, 0d);
doTestAppendScalar("", create(data1), getPreferredEntryValue(), 0d);
}
@Test
public void testGetSubVector() {
final double x = getPreferredEntryValue();
final double[] data = {x, x, x, 1d, x, 2d, x, x, 3d, x, x, x, 4d, x, x, x};
final int index = 1;
final int n = data.length - 5;
final RealVector actual = create(data).getSubVector(index, n);
final double[] expected = new double[n];
System.arraycopy(data, index, expected, 0, n);
TestUtils.assertEquals("", expected, actual, 0d);
}
@Test(expected = OutOfRangeException.class)
public void testGetSubVectorInvalidIndex1() {
final int n = 10;
create(new double[n]).getSubVector(-1, 2);
}
@Test(expected = OutOfRangeException.class)
public void testGetSubVectorInvalidIndex2() {
final int n = 10;
create(new double[n]).getSubVector(n, 2);
}
@Test(expected = OutOfRangeException.class)
public void testGetSubVectorInvalidIndex3() {
final int n = 10;
create(new double[n]).getSubVector(0, n + 1);
}
@Test(expected = NotPositiveException.class)
public void testGetSubVectorInvalidIndex4() {
final int n = 10;
create(new double[n]).getSubVector(3, -2);
}
@Test
public void testSetSubVectorSameType() {
final double x = getPreferredEntryValue();
final double[] expected = {x, x, x, 1d, x, 2d, x, x, 3d, x, x, x, 4d, x, x, x};
final
Math, 29
<FILEB>
<CHANGES>
final int n = getDimension();
for (int i = 0; i < n; i++) {
res.setEntry(i, this.getEntry(i) / v.getEntry(i));
<CHANGEE>
<CHANGES>
if (v.isNaN() || v.isInfinite()) {
final int n = getDimension();
for (int i = 0; i < n; i++) {
final double y = v.getEntry(i);
if (Double.isNaN(y)) {
res.setEntry(i, Double.NaN);
} else if (Double.isInfinite(y)) {
final double x = this.getEntry(i);
res.setEntry(i, x * y);
}
}
}
<CHANGEE>
<FILEE>
<FILEB>
@Override
public double dotProduct(RealVector v) {
if(v instanceof OpenMapRealVector) {
return dotProduct((OpenMapRealVector)v);
} else {
return super.dotProduct(v);
}
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeDivide(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
/*
* MATH-803: it is not sufficient to loop through non zero entries of
* this only. Indeed, if this[i] = 0d and v[i] = 0d, then
* this[i] / v[i] = NaN, and not 0d.
*/
<CHANGES>
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() / v.getEntry(iter.key()));
<CHANGEE>
}
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeMultiply(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() * v.getEntry(iter.key()));
}
/*
* MATH-803: the above loop assumes that 0d * x = 0d for any double x,
* which allows to consider only the non-zero entries of this. However,
* this fails if this[i] == 0d and (v[i] = NaN or v[i] = Infinity).
*
* These special cases are handled below.
*/
<CHANGES>
<CHANGEE>
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector getSubVector(int index, int n) {
checkIndex(index);
if (n < 0) {
throw new NotPositiveException(LocalizedFormats.NUMBER_OF_ELEMENTS_SHOULD_BE_POSITIVE, n);
}
checkIndex(index + n - 1);
OpenMapRealVector<SCANS> double[] sub = {5d, x, 6d, 7d, 8d};
final RealVector actual = create(expected);
final int index = 2;
actual.setSubVector(index, create(sub));
for (int i = 0; i < sub.length; i++){
expected[index + i] = sub[i];
}
TestUtils.assertEquals("", expected, actual, 0d);
}
@Test
public void testSetSubVectorMixedType() {
final double x = getPreferredEntryValue();
final double[] expected = {x, x, x, 1d, x, 2d, x, x, 3d, x, x, x, 4d, x, x, x};
final double[] sub = {5d, x, 6d, 7d, 8d};
final RealVector actual = create(expected);
final int index = 2;
actual.setSubVector(index, createAlien(sub));
for (int i = 0; i < sub.length; i++){
expected[index + i] = sub[i];
}
TestUtils.assertEquals("", expected, actual, 0d);
}
@Test(expected = OutOfRangeException.class)
public void testSetSubVectorInvalidIndex1() {
create(new double[10]).setSubVector(-1, create(new double[2]));
}
@Test(expected = OutOfRangeException.class)
public void testSetSubVectorInvalidIndex2() {
create(new double[10]).setSubVector(10, create(new double[2]));
}
@Test(expected = OutOfRangeException.class)
public void testSetSubVectorInvalidIndex3() {
create(new double[10]).setSubVector(9, create(new double[2]));
}
@Test
public void testIsNaN() {
final RealVector v = create(new double[] {0, 1, 2});
Assert.assertFalse(v.isNaN());
v.setEntry(1, Double.NaN);
Assert.assertTrue(v.isNaN());
}
@Test
public void testIsInfinite() {
final RealVector v = create(new double[] { 0
Math, 29
<FILEB>
<CHANGES>
final int n = getDimension();
for (int i = 0; i < n; i++) {
res.setEntry(i, this.getEntry(i) / v.getEntry(i));
<CHANGEE>
<CHANGES>
if (v.isNaN() || v.isInfinite()) {
final int n = getDimension();
for (int i = 0; i < n; i++) {
final double y = v.getEntry(i);
if (Double.isNaN(y)) {
res.setEntry(i, Double.NaN);
} else if (Double.isInfinite(y)) {
final double x = this.getEntry(i);
res.setEntry(i, x * y);
}
}
}
<CHANGEE>
<FILEE>
<FILEB>
@Override
public double dotProduct(RealVector v) {
if(v instanceof OpenMapRealVector) {
return dotProduct((OpenMapRealVector)v);
} else {
return super.dotProduct(v);
}
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeDivide(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
/*
* MATH-803: it is not sufficient to loop through non zero entries of
* this only. Indeed, if this[i] = 0d and v[i] = 0d, then
* this[i] / v[i] = NaN, and not 0d.
*/
<CHANGES>
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() / v.getEntry(iter.key()));
<CHANGEE>
}
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeMultiply(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() * v.getEntry(iter.key()));
}
/*
* MATH-803: the above loop assumes that 0d * x = 0d for any double x,
* which allows to consider only the non-zero entries of this. However,
* this fails if this[i] == 0d and (v[i] = NaN or v[i] = Infinity).
*
* These special cases are handled below.
*/
<CHANGES>
<CHANGEE>
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector getSubVector(int index, int n) {
checkIndex(index);
if (n < 0) {
throw new NotPositiveException(LocalizedFormats.NUMBER_OF_ELEMENTS_SHOULD_BE_POSITIVE, n);
}
checkIndex(index + n - 1);
OpenMapRealVector<SCANS>, 1, 2 });
Assert.assertFalse(v.isInfinite());
v.setEntry(0, Double.POSITIVE_INFINITY);
Assert.assertTrue(v.isInfinite());
v.setEntry(1, Double.NaN);
Assert.assertFalse(v.isInfinite());
}
private void doTestEbeBinaryOperation(final BinaryOperation op, final boolean mixed) {
/*
* Make sure that x, y, z are three different values. Also, x is the
* preferred value (e.g. the value which is not stored in sparse
* implementations).
*/
final double x = getPreferredEntryValue();
final double y = x + 1d;
final double z = y + 1d;
/*
* This is an attempt at covering most particular cases of combining
* two values.
*
* 1. Addition
* --------
* The following cases should be covered
* (2 * x) + (-x)
* (-x) + 2 * x
* x + y
* y + x
* y + z
* y + (x - y)
* (y - x) + x
*
* The values to be considered are: x, y, z, 2 * x, -x, x - y, y - x.
*
* 2. Subtraction
* -----------
* The following cases should be covered
* (2 * x) - x
* x - y
* y - x
* y - z
* y - (y - x)
* (y + x) - y
*
* The values to be considered are: x, y, z, x + y, y - x.
*
* 3. Multiplication
* --------------
* (x * x) * (1 / x)
* (1 / x) * (x * x)
* x * y
* y * x
* y * z
*
* The values to be considered are: x, y, z, 1 / x, x * x.
*
* 4. Division
* --------
* (x * x) / x
* x / y
* y / x
* y / z
*
* The values to be considered are: x,
Math, 29
<FILEB>
<CHANGES>
final int n = getDimension();
for (int i = 0; i < n; i++) {
res.setEntry(i, this.getEntry(i) / v.getEntry(i));
<CHANGEE>
<CHANGES>
if (v.isNaN() || v.isInfinite()) {
final int n = getDimension();
for (int i = 0; i < n; i++) {
final double y = v.getEntry(i);
if (Double.isNaN(y)) {
res.setEntry(i, Double.NaN);
} else if (Double.isInfinite(y)) {
final double x = this.getEntry(i);
res.setEntry(i, x * y);
}
}
}
<CHANGEE>
<FILEE>
<FILEB>
@Override
public double dotProduct(RealVector v) {
if(v instanceof OpenMapRealVector) {
return dotProduct((OpenMapRealVector)v);
} else {
return super.dotProduct(v);
}
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeDivide(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
/*
* MATH-803: it is not sufficient to loop through non zero entries of
* this only. Indeed, if this[i] = 0d and v[i] = 0d, then
* this[i] / v[i] = NaN, and not 0d.
*/
<CHANGES>
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() / v.getEntry(iter.key()));
<CHANGEE>
}
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeMultiply(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() * v.getEntry(iter.key()));
}
/*
* MATH-803: the above loop assumes that 0d * x = 0d for any double x,
* which allows to consider only the non-zero entries of this. However,
* this fails if this[i] == 0d and (v[i] = NaN or v[i] = Infinity).
*
* These special cases are handled below.
*/
<CHANGES>
<CHANGEE>
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector getSubVector(int index, int n) {
checkIndex(index);
if (n < 0) {
throw new NotPositiveException(LocalizedFormats.NUMBER_OF_ELEMENTS_SHOULD_BE_POSITIVE, n);
}
checkIndex(index + n - 1);
OpenMapRealVector<SCANS> y, z, x * x.
*
* Also to be considered NaN, POSITIVE_INFINITY, NEGATIVE_INFINITY.
*/
final double[] values = {x, y, z, 2 * x, -x, 1 / x, x * x, x + y, x - y, y - x};
final double[] data1 = new double[values.length * values.length];
final double[] data2 = new double[values.length * values.length];
int k = 0;
for (int i = 0; i < values.length; i++) {
for (int j = 0; j < values.length; j++) {
data1[k] = values[i];
data2[k] = values[j];
++k;
}
}
final RealVector v1 = create(data1);
final RealVector v2 = mixed ? createAlien(data2) : create(data2);
final RealVector actual;
switch (op) {
case ADD:
actual = v1.add(v2);
break;
case SUB:
actual = v1.subtract(v2);
break;
case MUL:
actual = v1.ebeMultiply(v2);
break;
case DIV:
actual = v1.ebeDivide(v2);
break;
default:
throw new AssertionError("unexpected value");
}
final double[] expected = new double[data1.length];
for (int i = 0; i < expected.length; i++) {
switch (op) {
case ADD:
expected[i] = data1[i] + data2[i];
break;
case SUB:
expected[i] = data1[i] - data2[i];
break;
case MUL:
expected[i] = data1[i] * data2[i];
break;
case DIV:
expected[i] = data1[i] / data2[i];
break;
default:
throw new AssertionError("unexpected value");
}
}
for (int i = 0; i < expected.length; i++) {
final String msg = "entry #"+i+", left = "+data1[i]+", right = " + data2[
Math, 29
<FILEB>
<CHANGES>
final int n = getDimension();
for (int i = 0; i < n; i++) {
res.setEntry(i, this.getEntry(i) / v.getEntry(i));
<CHANGEE>
<CHANGES>
if (v.isNaN() || v.isInfinite()) {
final int n = getDimension();
for (int i = 0; i < n; i++) {
final double y = v.getEntry(i);
if (Double.isNaN(y)) {
res.setEntry(i, Double.NaN);
} else if (Double.isInfinite(y)) {
final double x = this.getEntry(i);
res.setEntry(i, x * y);
}
}
}
<CHANGEE>
<FILEE>
<FILEB>
@Override
public double dotProduct(RealVector v) {
if(v instanceof OpenMapRealVector) {
return dotProduct((OpenMapRealVector)v);
} else {
return super.dotProduct(v);
}
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeDivide(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
/*
* MATH-803: it is not sufficient to loop through non zero entries of
* this only. Indeed, if this[i] = 0d and v[i] = 0d, then
* this[i] / v[i] = NaN, and not 0d.
*/
<CHANGES>
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() / v.getEntry(iter.key()));
<CHANGEE>
}
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeMultiply(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() * v.getEntry(iter.key()));
}
/*
* MATH-803: the above loop assumes that 0d * x = 0d for any double x,
* which allows to consider only the non-zero entries of this. However,
* this fails if this[i] == 0d and (v[i] = NaN or v[i] = Infinity).
*
* These special cases are handled below.
*/
<CHANGES>
<CHANGEE>
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector getSubVector(int index, int n) {
checkIndex(index);
if (n < 0) {
throw new NotPositiveException(LocalizedFormats.NUMBER_OF_ELEMENTS_SHOULD_BE_POSITIVE, n);
}
checkIndex(index + n - 1);
OpenMapRealVector<SCANS>i];
Assert.assertEquals(msg, expected[i], actual.getEntry(i), 0.0);
}
}
private void doTestEbeBinaryOperationDimensionMismatch(final BinaryOperation op) {
final int n = 10;
switch (op) {
case ADD:
create(new double[n]).add(create(new double[n + 1]));
break;
case SUB:
create(new double[n]).subtract(create(new double[n + 1]));
break;
case MUL:
create(new double[n]).ebeMultiply(create(new double[n + 1]));
break;
case DIV:
create(new double[n]).ebeDivide(create(new double[n + 1]));
break;
default:
throw new AssertionError("unexpected value");
}
}
@Test
public void testAddSameType() {
doTestEbeBinaryOperation(BinaryOperation.ADD, false);
}
@Test
public void testAddMixedTypes() {
doTestEbeBinaryOperation(BinaryOperation.ADD, true);
}
@Test(expected = DimensionMismatchException.class)
public void testAddDimensionMismatch() {
doTestEbeBinaryOperationDimensionMismatch(BinaryOperation.ADD);
}
@Test
public void testSubtractSameType() {
doTestEbeBinaryOperation(BinaryOperation.SUB, false);
}
@Test
public void testSubtractMixedTypes() {
doTestEbeBinaryOperation(BinaryOperation.SUB, true);
}
@Test(expected = DimensionMismatchException.class)
public void testSubtractDimensionMismatch() {
doTestEbeBinaryOperationDimensionMismatch(BinaryOperation.SUB);
}
@Test
public void testEbeMultiplySameType() {
doTestEbeBinaryOperation(BinaryOperation.MUL, false);
}
@Test
public void testEbeMultiplyMixedTypes() {
doTestEbeBinaryOperation(BinaryOperation.MUL, true);
}
@Test(expected = DimensionMismatchException.class)
public void testEbeMultiplyDimensionMismatch() {
doTestEbeBinaryOperationDimensionMismatch(BinaryOperation.MUL);
}
@Test
public void testEbeDivideSameType() {
doTestEbeBinaryOperation(BinaryOperation.DIV, false);
Math, 29
<FILEB>
<CHANGES>
final int n = getDimension();
for (int i = 0; i < n; i++) {
res.setEntry(i, this.getEntry(i) / v.getEntry(i));
<CHANGEE>
<CHANGES>
if (v.isNaN() || v.isInfinite()) {
final int n = getDimension();
for (int i = 0; i < n; i++) {
final double y = v.getEntry(i);
if (Double.isNaN(y)) {
res.setEntry(i, Double.NaN);
} else if (Double.isInfinite(y)) {
final double x = this.getEntry(i);
res.setEntry(i, x * y);
}
}
}
<CHANGEE>
<FILEE>
<FILEB>
@Override
public double dotProduct(RealVector v) {
if(v instanceof OpenMapRealVector) {
return dotProduct((OpenMapRealVector)v);
} else {
return super.dotProduct(v);
}
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeDivide(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
/*
* MATH-803: it is not sufficient to loop through non zero entries of
* this only. Indeed, if this[i] = 0d and v[i] = 0d, then
* this[i] / v[i] = NaN, and not 0d.
*/
<CHANGES>
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() / v.getEntry(iter.key()));
<CHANGEE>
}
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeMultiply(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() * v.getEntry(iter.key()));
}
/*
* MATH-803: the above loop assumes that 0d * x = 0d for any double x,
* which allows to consider only the non-zero entries of this. However,
* this fails if this[i] == 0d and (v[i] = NaN or v[i] = Infinity).
*
* These special cases are handled below.
*/
<CHANGES>
<CHANGEE>
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector getSubVector(int index, int n) {
checkIndex(index);
if (n < 0) {
throw new NotPositiveException(LocalizedFormats.NUMBER_OF_ELEMENTS_SHOULD_BE_POSITIVE, n);
}
checkIndex(index + n - 1);
OpenMapRealVector<SCANS> v1.getMaxIndex());
Assert.assertEquals(12, v1.getMaxValue(), 1.0e-12);
final RealVector v2 = create(new double[] {Double.NaN, 3, Double.NaN, -2});
Assert.assertEquals(3, v2.getMinIndex());
Assert.assertEquals(-2, v2.getMinValue(), 1.0e-12);
Assert.assertEquals(1, v2.getMaxIndex());
Assert.assertEquals(3, v2.getMaxValue(), 1.0e-12);
final RealVector v3 = create(new double[] {Double.NaN, Double.NaN});
Assert.assertEquals(-1, v3.getMinIndex());
Assert.assertTrue(Double.isNaN(v3.getMinValue()));
Assert.assertEquals(-1, v3.getMaxIndex());
Assert.assertTrue(Double.isNaN(v3.getMaxValue()));
final RealVector v4 = create(new double[0]);
Assert.assertEquals(-1, v4.getMinIndex());
Assert.assertTrue(Double.isNaN(v4.getMinValue()));
Assert.assertEquals(-1, v4.getMaxIndex());
Assert.assertTrue(Double.isNaN(v4.getMaxValue()));
}
@Test
public void testCosine() {
final RealVector v = create(new double[] {1, 0, 0});
double[] wData = new double[] {1, 1, 0};
RealVector w = create(wData);
Assert.assertEquals(FastMath.sqrt(2) / 2, v.cosine(w), normTolerance);
wData = new double[] {1, 0, 0};
w = create(wData);
Assert.assertEquals(1, v.cosine(w), normTolerance);
wData = new double[] {0, 1, 0};
w = create(wData);
Assert.assertEquals(0, v.cosine(w), 0);
wData = new double[] {-1, 0, 0};
w = create(wData);
Assert.assertEquals(-1, v.cosine(w), normTolerance);
}
@Test(expected=MathArithmeticException.class)
public void testCosinePrecondition1() {
final
Math, 29
<FILEB>
<CHANGES>
final int n = getDimension();
for (int i = 0; i < n; i++) {
res.setEntry(i, this.getEntry(i) / v.getEntry(i));
<CHANGEE>
<CHANGES>
if (v.isNaN() || v.isInfinite()) {
final int n = getDimension();
for (int i = 0; i < n; i++) {
final double y = v.getEntry(i);
if (Double.isNaN(y)) {
res.setEntry(i, Double.NaN);
} else if (Double.isInfinite(y)) {
final double x = this.getEntry(i);
res.setEntry(i, x * y);
}
}
}
<CHANGEE>
<FILEE>
<FILEB>
@Override
public double dotProduct(RealVector v) {
if(v instanceof OpenMapRealVector) {
return dotProduct((OpenMapRealVector)v);
} else {
return super.dotProduct(v);
}
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeDivide(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
/*
* MATH-803: it is not sufficient to loop through non zero entries of
* this only. Indeed, if this[i] = 0d and v[i] = 0d, then
* this[i] / v[i] = NaN, and not 0d.
*/
<CHANGES>
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() / v.getEntry(iter.key()));
<CHANGEE>
}
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeMultiply(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() * v.getEntry(iter.key()));
}
/*
* MATH-803: the above loop assumes that 0d * x = 0d for any double x,
* which allows to consider only the non-zero entries of this. However,
* this fails if this[i] == 0d and (v[i] = NaN or v[i] = Infinity).
*
* These special cases are handled below.
*/
<CHANGES>
<CHANGEE>
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector getSubVector(int index, int n) {
checkIndex(index);
if (n < 0) {
throw new NotPositiveException(LocalizedFormats.NUMBER_OF_ELEMENTS_SHOULD_BE_POSITIVE, n);
}
checkIndex(index + n - 1);
OpenMapRealVector<SCANS> RealVector v = create(new double[] {0, 0, 0});
final RealVector w = create(new double[] {1, 0, 0});
v.cosine(w);
}
@Test(expected=MathArithmeticException.class)
public void testCosinePrecondition2() {
final RealVector v = create(new double[] {0, 0, 0});
final RealVector w = create(new double[] {1, 0, 0});
w.cosine(v);
}
@Test(expected=DimensionMismatchException.class)
public void testCosinePrecondition3() {
final RealVector v = create(new double[] {1, 2, 3});
final RealVector w = create(new double[] {1, 2, 3, 4});
v.cosine(w);
}
@Test(expected=DimensionMismatchException.class)
public void testCombinePreconditionSameType() {
final double a = 1d;
final double b = 2d;
double[] aux = new double[] { 3d, 4d, 5d };
final RealVector x = create(aux);
aux = new double[] { 6d, 7d };
final RealVector y = create(aux);
x.combine(a, b, y);
}
@Test
public void testCombineSameType() {
final Random random = new Random(20110726);
final int dim = 10;
final double a = (2 * random.nextDouble() - 1);
final double b = (2 * random.nextDouble() - 1);
final double[] dataX = new double[dim];
final double[] dataY = new double[dim];
final double[] expected = new double[dim];
for (int i = 0; i < dim; i++) {
dataX[i] = 2 * random.nextDouble() - 1;
dataY[i] = 2 * random.nextDouble() - 1;
expected[i] = a * dataX[i] + b * dataY[i];
}
final RealVector x = create(dataX);
final RealVector y = create(dataY
Math, 29
<FILEB>
<CHANGES>
final int n = getDimension();
for (int i = 0; i < n; i++) {
res.setEntry(i, this.getEntry(i) / v.getEntry(i));
<CHANGEE>
<CHANGES>
if (v.isNaN() || v.isInfinite()) {
final int n = getDimension();
for (int i = 0; i < n; i++) {
final double y = v.getEntry(i);
if (Double.isNaN(y)) {
res.setEntry(i, Double.NaN);
} else if (Double.isInfinite(y)) {
final double x = this.getEntry(i);
res.setEntry(i, x * y);
}
}
}
<CHANGEE>
<FILEE>
<FILEB>
@Override
public double dotProduct(RealVector v) {
if(v instanceof OpenMapRealVector) {
return dotProduct((OpenMapRealVector)v);
} else {
return super.dotProduct(v);
}
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeDivide(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
/*
* MATH-803: it is not sufficient to loop through non zero entries of
* this only. Indeed, if this[i] = 0d and v[i] = 0d, then
* this[i] / v[i] = NaN, and not 0d.
*/
<CHANGES>
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() / v.getEntry(iter.key()));
<CHANGEE>
}
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeMultiply(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() * v.getEntry(iter.key()));
}
/*
* MATH-803: the above loop assumes that 0d * x = 0d for any double x,
* which allows to consider only the non-zero entries of this. However,
* this fails if this[i] == 0d and (v[i] = NaN or v[i] = Infinity).
*
* These special cases are handled below.
*/
<CHANGES>
<CHANGEE>
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector getSubVector(int index, int n) {
checkIndex(index);
if (n < 0) {
throw new NotPositiveException(LocalizedFormats.NUMBER_OF_ELEMENTS_SHOULD_BE_POSITIVE, n);
}
checkIndex(index + n - 1);
OpenMapRealVector<SCANS>);
final double[] actual = x.combine(a, b, y).toArray();
for (int i = 0; i < dim; i++) {
final double delta;
if (expected[i] == 0d) {
delta = Math.ulp(1d);
} else {
delta = Math.ulp(expected[i]);
}
Assert.assertEquals("elements [" + i + "] differ",
expected[i],
actual[i],
delta);
}
}
@Test(expected=DimensionMismatchException.class)
public void testCombinePreconditionMixedType() {
final double a = 1d;
final double b = 2d;
double[] aux = new double[] { 3d, 4d, 5d };
final RealVector x = create(aux);
aux = new double[] { 6d, 7d };
final RealVector y = create(aux);
x.combine(a, b, y);
}
@Test
public void testCombineMixedTypes() {
final Random random = new Random(20110726);
final int dim = 10;
final double a = (2 * random.nextDouble() - 1);
final double b = (2 * random.nextDouble() - 1);
final double[] dataX = new double[dim];
final double[] dataY = new double[dim];
final double[] expected = new double[dim];
for (int i = 0; i < dim; i++) {
dataX[i] = 2 * random.nextDouble() - 1;
dataY[i] = 2 * random.nextDouble() - 1;
expected[i] = a * dataX[i] + b * dataY[i];
}
final RealVector x = create(dataX);
final RealVector y = createAlien(dataY);
final double[] actual = x.combine(a, b, y).toArray();
for (int i = 0; i < dim; i++) {
final double delta;
if (expected[i] == 0d) {
delta = Math.ulp(1d);
} else {
delta = Math.ulp(expected[i]);
}
Assert.
Math, 29
<FILEB>
<CHANGES>
final int n = getDimension();
for (int i = 0; i < n; i++) {
res.setEntry(i, this.getEntry(i) / v.getEntry(i));
<CHANGEE>
<CHANGES>
if (v.isNaN() || v.isInfinite()) {
final int n = getDimension();
for (int i = 0; i < n; i++) {
final double y = v.getEntry(i);
if (Double.isNaN(y)) {
res.setEntry(i, Double.NaN);
} else if (Double.isInfinite(y)) {
final double x = this.getEntry(i);
res.setEntry(i, x * y);
}
}
}
<CHANGEE>
<FILEE>
<FILEB>
@Override
public double dotProduct(RealVector v) {
if(v instanceof OpenMapRealVector) {
return dotProduct((OpenMapRealVector)v);
} else {
return super.dotProduct(v);
}
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeDivide(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
/*
* MATH-803: it is not sufficient to loop through non zero entries of
* this only. Indeed, if this[i] = 0d and v[i] = 0d, then
* this[i] / v[i] = NaN, and not 0d.
*/
<CHANGES>
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() / v.getEntry(iter.key()));
<CHANGEE>
}
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeMultiply(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() * v.getEntry(iter.key()));
}
/*
* MATH-803: the above loop assumes that 0d * x = 0d for any double x,
* which allows to consider only the non-zero entries of this. However,
* this fails if this[i] == 0d and (v[i] = NaN or v[i] = Infinity).
*
* These special cases are handled below.
*/
<CHANGES>
<CHANGEE>
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector getSubVector(int index, int n) {
checkIndex(index);
if (n < 0) {
throw new NotPositiveException(LocalizedFormats.NUMBER_OF_ELEMENTS_SHOULD_BE_POSITIVE, n);
}
checkIndex(index + n - 1);
OpenMapRealVector<SCANS>assertEquals("elements [" + i + "] differ",
expected[i],
actual[i],
delta);
}
}
@Test(expected=DimensionMismatchException.class)
public void testCombineToSelfPreconditionSameType() {
final double a = 1d;
final double b = 2d;
double[] aux = new double[] { 3d, 4d, 5d };
final RealVector x = create(aux);
aux = new double[] { 6d, 7d };
final RealVector y = create(aux);
x.combineToSelf(a, b, y);
}
@Test
public void testCombineToSelfSameType() {
final Random random = new Random(20110726);
final int dim = 10;
final double a = (2 * random.nextDouble() - 1);
final double b = (2 * random.nextDouble() - 1);
final double[] dataX = new double[dim];
final double[] dataY = new double[dim];
final double[] expected = new double[dim];
for (int i = 0; i < dim; i++) {
dataX[i] = 2 * random.nextDouble() - 1;
dataY[i] = 2 * random.nextDouble() - 1;
expected[i] = a * dataX[i] + b * dataY[i];
}
final RealVector x = create(dataX);
final RealVector y = create(dataY);
Assert.assertSame(x, x.combineToSelf(a, b, y));
final double[] actual = x.toArray();
for (int i = 0; i < dim; i++) {
final double delta;
if (expected[i] == 0d) {
delta = Math.ulp(1d);
} else {
delta = Math.ulp(expected[i]);
}
Assert.assertEquals("elements [" + i + "] differ",
expected[i],
actual[i],
delta);
}
}
@Test(expected=DimensionMismatchException.class)
public void testCombineToSelfPreconditionMixedType() {
final double a = 1d;
final double b = 2d
Math, 29
<FILEB>
<CHANGES>
final int n = getDimension();
for (int i = 0; i < n; i++) {
res.setEntry(i, this.getEntry(i) / v.getEntry(i));
<CHANGEE>
<CHANGES>
if (v.isNaN() || v.isInfinite()) {
final int n = getDimension();
for (int i = 0; i < n; i++) {
final double y = v.getEntry(i);
if (Double.isNaN(y)) {
res.setEntry(i, Double.NaN);
} else if (Double.isInfinite(y)) {
final double x = this.getEntry(i);
res.setEntry(i, x * y);
}
}
}
<CHANGEE>
<FILEE>
<FILEB>
@Override
public double dotProduct(RealVector v) {
if(v instanceof OpenMapRealVector) {
return dotProduct((OpenMapRealVector)v);
} else {
return super.dotProduct(v);
}
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeDivide(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
/*
* MATH-803: it is not sufficient to loop through non zero entries of
* this only. Indeed, if this[i] = 0d and v[i] = 0d, then
* this[i] / v[i] = NaN, and not 0d.
*/
<CHANGES>
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() / v.getEntry(iter.key()));
<CHANGEE>
}
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeMultiply(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() * v.getEntry(iter.key()));
}
/*
* MATH-803: the above loop assumes that 0d * x = 0d for any double x,
* which allows to consider only the non-zero entries of this. However,
* this fails if this[i] == 0d and (v[i] = NaN or v[i] = Infinity).
*
* These special cases are handled below.
*/
<CHANGES>
<CHANGEE>
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector getSubVector(int index, int n) {
checkIndex(index);
if (n < 0) {
throw new NotPositiveException(LocalizedFormats.NUMBER_OF_ELEMENTS_SHOULD_BE_POSITIVE, n);
}
checkIndex(index + n - 1);
OpenMapRealVector<SCANS>;
double[] aux = new double[] { 3d, 4d, 5d };
final RealVector x = create(aux);
aux = new double[] { 6d, 7d };
final RealVector y = createAlien(aux);
x.combineToSelf(a, b, y);
}
@Test
public void testCombineToSelfMixedTypes() {
final Random random = new Random(20110726);
final int dim = 10;
final double a = (2 * random.nextDouble() - 1);
final double b = (2 * random.nextDouble() - 1);
final double[] dataX = new double[dim];
final double[] dataY = new double[dim];
final double[] expected = new double[dim];
for (int i = 0; i < dim; i++) {
dataX[i] = 2 * random.nextDouble() - 1;
dataY[i] = 2 * random.nextDouble() - 1;
expected[i] = a * dataX[i] + b * dataY[i];
}
final RealVector x = create(dataX);
final RealVector y = create(dataY);
Assert.assertSame(x, x.combineToSelf(a, b, y));
final double[] actual = x.toArray();
for (int i = 0; i < dim; i++) {
final double delta;
if (expected[i] == 0d) {
delta = Math.ulp(1d);
} else {
delta = Math.ulp(expected[i]);
}
Assert.assertEquals("elements [" + i + "] differ",
expected[i],
actual[i],
delta);
}
}
/*
* TESTS OF THE VISITOR PATTERN
*/
/** The whole vector is visited. */
@Test
public void testWalkInDefaultOrderPreservingVisitor1() {
final double[] data = new double[] {
0d, 1d, 0d, 0d, 2d, 0d, 0d, 0d, 3d
};
final RealVector v = create(data);
final RealVectorPreservingVisitor visitor;
Math, 29
<FILEB>
<CHANGES>
final int n = getDimension();
for (int i = 0; i < n; i++) {
res.setEntry(i, this.getEntry(i) / v.getEntry(i));
<CHANGEE>
<CHANGES>
if (v.isNaN() || v.isInfinite()) {
final int n = getDimension();
for (int i = 0; i < n; i++) {
final double y = v.getEntry(i);
if (Double.isNaN(y)) {
res.setEntry(i, Double.NaN);
} else if (Double.isInfinite(y)) {
final double x = this.getEntry(i);
res.setEntry(i, x * y);
}
}
}
<CHANGEE>
<FILEE>
<FILEB>
@Override
public double dotProduct(RealVector v) {
if(v instanceof OpenMapRealVector) {
return dotProduct((OpenMapRealVector)v);
} else {
return super.dotProduct(v);
}
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeDivide(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
/*
* MATH-803: it is not sufficient to loop through non zero entries of
* this only. Indeed, if this[i] = 0d and v[i] = 0d, then
* this[i] / v[i] = NaN, and not 0d.
*/
<CHANGES>
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() / v.getEntry(iter.key()));
<CHANGEE>
}
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeMultiply(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() * v.getEntry(iter.key()));
}
/*
* MATH-803: the above loop assumes that 0d * x = 0d for any double x,
* which allows to consider only the non-zero entries of this. However,
* this fails if this[i] == 0d and (v[i] = NaN or v[i] = Infinity).
*
* These special cases are handled below.
*/
<CHANGES>
<CHANGEE>
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector getSubVector(int index, int n) {
checkIndex(index);
if (n < 0) {
throw new NotPositiveException(LocalizedFormats.NUMBER_OF_ELEMENTS_SHOULD_BE_POSITIVE, n);
}
checkIndex(index + n - 1);
OpenMapRealVector<SCANS> for (int i = 0; i < data.length; i++) {
Assert.assertTrue("entry " + i + "has not been visited",
visited[i]);
}
return 0.0;
}
};
v.walkInOptimizedOrder(visitor);
}
/** Visiting an invalid subvector. */
@Test
public void testWalkInOptimizedOrderPreservingVisitor2() {
final RealVector v = create(new double[5]);
final RealVectorPreservingVisitor visitor;
visitor = new RealVectorPreservingVisitor() {
public void visit(int index, double value) {
// Do nothing
}
public void start(int dimension, int start, int end) {
// Do nothing
}
public double end() {
return 0.0;
}
};
try {
v.walkInOptimizedOrder(visitor, -1, 4);
Assert.fail();
} catch (OutOfRangeException e) {
// Expected behavior
}
try {
v.walkInOptimizedOrder(visitor, 5, 4);
Assert.fail();
} catch (OutOfRangeException e) {
// Expected behavior
}
try {
v.walkInOptimizedOrder(visitor, 0, -1);
Assert.fail();
} catch (OutOfRangeException e) {
// Expected behavior
}
try {
v.walkInOptimizedOrder(visitor, 0, 5);
Assert.fail();
} catch (OutOfRangeException e) {
// Expected behavior
}
try {
v.walkInOptimizedOrder(visitor, 4, 0);
Assert.fail();
} catch (NumberIsTooSmallException e) {
// Expected behavior
}
}
/** Visiting a valid subvector. */
@Test
public void testWalkInOptimizedOrderPreservingVisitor3() {
final double[] data = new double[] {
0d, 1d, 0d, 0d, 2d, 0d, 0d, 0d, 3d
};
final int expectedStart = 2;
final int expectedEnd = 7;
final RealVector v = create(data);
final RealVectorPreservingVisitor visitor;
visitor = new RealVectorPreservingVisitor() {
private final boolean
Math, 29
<FILEB>
<CHANGES>
final int n = getDimension();
for (int i = 0; i < n; i++) {
res.setEntry(i, this.getEntry(i) / v.getEntry(i));
<CHANGEE>
<CHANGES>
if (v.isNaN() || v.isInfinite()) {
final int n = getDimension();
for (int i = 0; i < n; i++) {
final double y = v.getEntry(i);
if (Double.isNaN(y)) {
res.setEntry(i, Double.NaN);
} else if (Double.isInfinite(y)) {
final double x = this.getEntry(i);
res.setEntry(i, x * y);
}
}
}
<CHANGEE>
<FILEE>
<FILEB>
@Override
public double dotProduct(RealVector v) {
if(v instanceof OpenMapRealVector) {
return dotProduct((OpenMapRealVector)v);
} else {
return super.dotProduct(v);
}
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeDivide(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
/*
* MATH-803: it is not sufficient to loop through non zero entries of
* this only. Indeed, if this[i] = 0d and v[i] = 0d, then
* this[i] / v[i] = NaN, and not 0d.
*/
<CHANGES>
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() / v.getEntry(iter.key()));
<CHANGEE>
}
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeMultiply(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() * v.getEntry(iter.key()));
}
/*
* MATH-803: the above loop assumes that 0d * x = 0d for any double x,
* which allows to consider only the non-zero entries of this. However,
* this fails if this[i] == 0d and (v[i] = NaN or v[i] = Infinity).
*
* These special cases are handled below.
*/
<CHANGES>
<CHANGEE>
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector getSubVector(int index, int n) {
checkIndex(index);
if (n < 0) {
throw new NotPositiveException(LocalizedFormats.NUMBER_OF_ELEMENTS_SHOULD_BE_POSITIVE, n);
}
checkIndex(index + n - 1);
OpenMapRealVector<SCANS>[] visited = new boolean[data.length];
public void visit(final int actualIndex, final double actualValue) {
Assert.assertEquals(Integer.toString(actualIndex),
data[actualIndex], actualValue, 0d);
visited[actualIndex] = true;
}
public void start(final int actualSize, final int actualStart,
final int actualEnd) {
Assert.assertEquals(data.length, actualSize);
Assert.assertEquals(expectedStart, actualStart);
Assert.assertEquals(expectedEnd, actualEnd);
Arrays.fill(visited, true);
}
public double end() {
for (int i = expectedStart; i <= expectedEnd; i++) {
Assert.assertTrue("entry " + i + "has not been visited",
visited[i]);
}
return 0.0;
}
};
v.walkInOptimizedOrder(visitor, expectedStart, expectedEnd);
}
/** The whole vector is visited. */
@Test
public void testWalkInDefaultOrderChangingVisitor1() {
final double[] data = new double[] {
0d, 1d, 0d, 0d, 2d, 0d, 0d, 0d, 3d
};
final RealVector v = create(data);
final RealVectorChangingVisitor visitor;
visitor = new RealVectorChangingVisitor() {
private int expectedIndex;
public double visit(final int actualIndex, final double actualValue) {
Assert.assertEquals(expectedIndex, actualIndex);
Assert.assertEquals(Integer.toString(actualIndex),
data[actualIndex], actualValue, 0d);
++expectedIndex;
return actualIndex + actualValue;
}
public void start(final int actualSize, final int actualStart,
final int actualEnd) {
Assert.assertEquals(data.length, actualSize);
Assert.assertEquals(0, actualStart);
Assert.assertEquals(data.length - 1, actualEnd);
expectedIndex = 0;
}
public double end() {
return 0.0;
}
};
v.walkInDefaultOrder(visitor);
for (int i = 0; i < data.length; i++) {
Assert.assertEquals("entry " + i, i + data[i], v.
Math, 29
<FILEB>
<CHANGES>
final int n = getDimension();
for (int i = 0; i < n; i++) {
res.setEntry(i, this.getEntry(i) / v.getEntry(i));
<CHANGEE>
<CHANGES>
if (v.isNaN() || v.isInfinite()) {
final int n = getDimension();
for (int i = 0; i < n; i++) {
final double y = v.getEntry(i);
if (Double.isNaN(y)) {
res.setEntry(i, Double.NaN);
} else if (Double.isInfinite(y)) {
final double x = this.getEntry(i);
res.setEntry(i, x * y);
}
}
}
<CHANGEE>
<FILEE>
<FILEB>
@Override
public double dotProduct(RealVector v) {
if(v instanceof OpenMapRealVector) {
return dotProduct((OpenMapRealVector)v);
} else {
return super.dotProduct(v);
}
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeDivide(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
/*
* MATH-803: it is not sufficient to loop through non zero entries of
* this only. Indeed, if this[i] = 0d and v[i] = 0d, then
* this[i] / v[i] = NaN, and not 0d.
*/
<CHANGES>
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() / v.getEntry(iter.key()));
<CHANGEE>
}
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeMultiply(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() * v.getEntry(iter.key()));
}
/*
* MATH-803: the above loop assumes that 0d * x = 0d for any double x,
* which allows to consider only the non-zero entries of this. However,
* this fails if this[i] == 0d and (v[i] = NaN or v[i] = Infinity).
*
* These special cases are handled below.
*/
<CHANGES>
<CHANGEE>
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector getSubVector(int index, int n) {
checkIndex(index);
if (n < 0) {
throw new NotPositiveException(LocalizedFormats.NUMBER_OF_ELEMENTS_SHOULD_BE_POSITIVE, n);
}
checkIndex(index + n - 1);
OpenMapRealVector<SCANS>getEntry(i), 0.0);
}
}
/** Visiting an invalid subvector. */
@Test
public void testWalkInDefaultOrderChangingVisitor2() {
final RealVector v = create(new double[5]);
final RealVectorChangingVisitor visitor;
visitor = new RealVectorChangingVisitor() {
public double visit(int index, double value) {
return 0.0;
}
public void start(int dimension, int start, int end) {
// Do nothing
}
public double end() {
return 0.0;
}
};
try {
v.walkInDefaultOrder(visitor, -1, 4);
Assert.fail();
} catch (OutOfRangeException e) {
// Expected behavior
}
try {
v.walkInDefaultOrder(visitor, 5, 4);
Assert.fail();
} catch (OutOfRangeException e) {
// Expected behavior
}
try {
v.walkInDefaultOrder(visitor, 0, -1);
Assert.fail();
} catch (OutOfRangeException e) {
// Expected behavior
}
try {
v.walkInDefaultOrder(visitor, 0, 5);
Assert.fail();
} catch (OutOfRangeException e) {
// Expected behavior
}
try {
v.walkInDefaultOrder(visitor, 4, 0);
Assert.fail();
} catch (NumberIsTooSmallException e) {
// Expected behavior
}
}
/** Visiting a valid subvector. */
@Test
public void testWalkInDefaultOrderChangingVisitor3() {
final double[] data = new double[] {
0d, 1d, 0d, 0d, 2d, 0d, 0d, 0d, 3d
};
final int expectedStart = 2;
final int expectedEnd = 7;
final RealVector v = create(data);
final RealVectorChangingVisitor visitor;
visitor = new RealVectorChangingVisitor() {
private int expectedIndex;
public double visit(final int actualIndex, final double actualValue) {
Assert.assertEquals(expectedIndex, actualIndex);
Assert.assertEquals(Integer.toString(actualIndex),
data[actualIndex], actualValue, 0d);
++expectedIndex;
return
Math, 29
<FILEB>
<CHANGES>
final int n = getDimension();
for (int i = 0; i < n; i++) {
res.setEntry(i, this.getEntry(i) / v.getEntry(i));
<CHANGEE>
<CHANGES>
if (v.isNaN() || v.isInfinite()) {
final int n = getDimension();
for (int i = 0; i < n; i++) {
final double y = v.getEntry(i);
if (Double.isNaN(y)) {
res.setEntry(i, Double.NaN);
} else if (Double.isInfinite(y)) {
final double x = this.getEntry(i);
res.setEntry(i, x * y);
}
}
}
<CHANGEE>
<FILEE>
<FILEB>
@Override
public double dotProduct(RealVector v) {
if(v instanceof OpenMapRealVector) {
return dotProduct((OpenMapRealVector)v);
} else {
return super.dotProduct(v);
}
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeDivide(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
/*
* MATH-803: it is not sufficient to loop through non zero entries of
* this only. Indeed, if this[i] = 0d and v[i] = 0d, then
* this[i] / v[i] = NaN, and not 0d.
*/
<CHANGES>
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() / v.getEntry(iter.key()));
<CHANGEE>
}
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeMultiply(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() * v.getEntry(iter.key()));
}
/*
* MATH-803: the above loop assumes that 0d * x = 0d for any double x,
* which allows to consider only the non-zero entries of this. However,
* this fails if this[i] == 0d and (v[i] = NaN or v[i] = Infinity).
*
* These special cases are handled below.
*/
<CHANGES>
<CHANGEE>
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector getSubVector(int index, int n) {
checkIndex(index);
if (n < 0) {
throw new NotPositiveException(LocalizedFormats.NUMBER_OF_ELEMENTS_SHOULD_BE_POSITIVE, n);
}
checkIndex(index + n - 1);
OpenMapRealVector<SCANS> actualIndex + actualValue;
}
public void start(final int actualSize, final int actualStart,
final int actualEnd) {
Assert.assertEquals(data.length, actualSize);
Assert.assertEquals(expectedStart, actualStart);
Assert.assertEquals(expectedEnd, actualEnd);
expectedIndex = expectedStart;
}
public double end() {
return 0.0;
}
};
v.walkInDefaultOrder(visitor, expectedStart, expectedEnd);
for (int i = expectedStart; i <= expectedEnd; i++) {
Assert.assertEquals("entry " + i, i + data[i], v.getEntry(i), 0.0);
}
}
/** The whole vector is visited. */
@Test
public void testWalkInOptimizedOrderChangingVisitor1() {
final double[] data = new double[] {
0d, 1d, 0d, 0d, 2d, 0d, 0d, 0d, 3d
};
final RealVector v = create(data);
final RealVectorChangingVisitor visitor;
visitor = new RealVectorChangingVisitor() {
private final boolean[] visited = new boolean[data.length];
public double visit(final int actualIndex, final double actualValue) {
visited[actualIndex] = true;
Assert.assertEquals(Integer.toString(actualIndex),
data[actualIndex], actualValue, 0d);
return actualIndex + actualValue;
}
public void start(final int actualSize, final int actualStart,
final int actualEnd) {
Assert.assertEquals(data.length, actualSize);
Assert.assertEquals(0, actualStart);
Assert.assertEquals(data.length - 1, actualEnd);
Arrays.fill(visited, false);
}
public double end() {
for (int i = 0; i < data.length; i++) {
Assert.assertTrue("entry " + i + "has not been visited",
visited[i]);
}
return 0.0;
}
};
v.walkInOptimizedOrder(visitor);
for (int i = 0; i < data.length; i++) {
Assert.assertEquals("entry " + i, i + data[i], v.getEntry(i),
Math, 29
<FILEB>
<CHANGES>
final int n = getDimension();
for (int i = 0; i < n; i++) {
res.setEntry(i, this.getEntry(i) / v.getEntry(i));
<CHANGEE>
<CHANGES>
if (v.isNaN() || v.isInfinite()) {
final int n = getDimension();
for (int i = 0; i < n; i++) {
final double y = v.getEntry(i);
if (Double.isNaN(y)) {
res.setEntry(i, Double.NaN);
} else if (Double.isInfinite(y)) {
final double x = this.getEntry(i);
res.setEntry(i, x * y);
}
}
}
<CHANGEE>
<FILEE>
<FILEB>
@Override
public double dotProduct(RealVector v) {
if(v instanceof OpenMapRealVector) {
return dotProduct((OpenMapRealVector)v);
} else {
return super.dotProduct(v);
}
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeDivide(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
/*
* MATH-803: it is not sufficient to loop through non zero entries of
* this only. Indeed, if this[i] = 0d and v[i] = 0d, then
* this[i] / v[i] = NaN, and not 0d.
*/
<CHANGES>
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() / v.getEntry(iter.key()));
<CHANGEE>
}
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeMultiply(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() * v.getEntry(iter.key()));
}
/*
* MATH-803: the above loop assumes that 0d * x = 0d for any double x,
* which allows to consider only the non-zero entries of this. However,
* this fails if this[i] == 0d and (v[i] = NaN or v[i] = Infinity).
*
* These special cases are handled below.
*/
<CHANGES>
<CHANGEE>
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector getSubVector(int index, int n) {
checkIndex(index);
if (n < 0) {
throw new NotPositiveException(LocalizedFormats.NUMBER_OF_ELEMENTS_SHOULD_BE_POSITIVE, n);
}
checkIndex(index + n - 1);
OpenMapRealVector<SCANS>;
return actualIndex + actualValue;
}
public void start(final int actualSize, final int actualStart,
final int actualEnd) {
Assert.assertEquals(data.length, actualSize);
Assert.assertEquals(expectedStart, actualStart);
Assert.assertEquals(expectedEnd, actualEnd);
Arrays.fill(visited, true);
}
public double end() {
for (int i = expectedStart; i <= expectedEnd; i++) {
Assert.assertTrue("entry " + i + "has not been visited",
visited[i]);
}
return 0.0;
}
};
v.walkInOptimizedOrder(visitor, expectedStart, expectedEnd);
for (int i = expectedStart; i <= expectedEnd; i++) {
Assert.assertEquals("entry " + i, i + data[i], v.getEntry(i), 0.0);
}
}
}
Math, 29
<FILEB>
<CHANGES>
final int n = getDimension();
for (int i = 0; i < n; i++) {
res.setEntry(i, this.getEntry(i) / v.getEntry(i));
<CHANGEE>
<CHANGES>
if (v.isNaN() || v.isInfinite()) {
final int n = getDimension();
for (int i = 0; i < n; i++) {
final double y = v.getEntry(i);
if (Double.isNaN(y)) {
res.setEntry(i, Double.NaN);
} else if (Double.isInfinite(y)) {
final double x = this.getEntry(i);
res.setEntry(i, x * y);
}
}
}
<CHANGEE>
<FILEE>
<FILEB>
@Override
public double dotProduct(RealVector v) {
if(v instanceof OpenMapRealVector) {
return dotProduct((OpenMapRealVector)v);
} else {
return super.dotProduct(v);
}
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeDivide(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
/*
* MATH-803: it is not sufficient to loop through non zero entries of
* this only. Indeed, if this[i] = 0d and v[i] = 0d, then
* this[i] / v[i] = NaN, and not 0d.
*/
<CHANGES>
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() / v.getEntry(iter.key()));
<CHANGEE>
}
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeMultiply(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() * v.getEntry(iter.key()));
}
/*
* MATH-803: the above loop assumes that 0d * x = 0d for any double x,
* which allows to consider only the non-zero entries of this. However,
* this fails if this[i] == 0d and (v[i] = NaN or v[i] = Infinity).
*
* These special cases are handled below.
*/
<CHANGES>
<CHANGEE>
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector getSubVector(int index, int n) {
checkIndex(index);
if (n < 0) {
throw new NotPositiveException(LocalizedFormats.NUMBER_OF_ELEMENTS_SHOULD_BE_POSITIVE, n);
}
checkIndex(index + n - 1);
OpenMapRealVector<SCANS>
* @param d Array.
* @param pos Position of first entry.
* @param size Number of entries to copy.
* @throws NullArgumentException if {@code d} is {@code null}.
* @throws NumberIsTooLargeException if the size of {@code d} is less
* than {@code pos + size}.
*/
public ArrayRealVector(double[] d, int pos, int size) {
if (d == null) {
throw new NullArgumentException();
}
if (d.length < pos + size) {
throw new NumberIsTooLargeException(pos + size, d.length, true);
}
data = new double[size];
System.arraycopy(d, pos, data, 0, size);
}
/**
* Construct a vector from an array.
*
* @param d Array of {@code Double}s.
*/
public ArrayRealVector(Double[] d) {
data = new double[d.length];
for (int i = 0; i < d.length; i++) {
data[i] = d[i].doubleValue();
}
}
/**
* Construct a vector from part of an array.
*
* @param d Array.
* @param pos Position of first entry.
* @param size Number of entries to copy.
* @throws NullArgumentException if {@code d} is {@code null}.
* @throws NumberIsTooLargeException if the size of {@code d} is less
* than {@code pos + size}.
*/
public ArrayRealVector(Double[] d, int pos, int size) {
if (d == null) {
throw new NullArgumentException();
}
if (d.length < pos + size) {
throw new NumberIsTooLargeException(pos + size, d.length, true);
}
data = new double[size];
for (int i = pos; i < pos + size; i++) {
data[i - pos] = d[i].doubleValue();
}
}
/**
* Construct a vector from another vector, using a deep copy.
*
* @param v vector to copy.
* @throws NullArgumentException if {@code v} is {@code null}.
*/
public ArrayRealVector(RealVector v) {
if (v == null
Math, 29
<FILEB>
<CHANGES>
final int n = getDimension();
for (int i = 0; i < n; i++) {
res.setEntry(i, this.getEntry(i) / v.getEntry(i));
<CHANGEE>
<CHANGES>
if (v.isNaN() || v.isInfinite()) {
final int n = getDimension();
for (int i = 0; i < n; i++) {
final double y = v.getEntry(i);
if (Double.isNaN(y)) {
res.setEntry(i, Double.NaN);
} else if (Double.isInfinite(y)) {
final double x = this.getEntry(i);
res.setEntry(i, x * y);
}
}
}
<CHANGEE>
<FILEE>
<FILEB>
@Override
public double dotProduct(RealVector v) {
if(v instanceof OpenMapRealVector) {
return dotProduct((OpenMapRealVector)v);
} else {
return super.dotProduct(v);
}
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeDivide(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
/*
* MATH-803: it is not sufficient to loop through non zero entries of
* this only. Indeed, if this[i] = 0d and v[i] = 0d, then
* this[i] / v[i] = NaN, and not 0d.
*/
<CHANGES>
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() / v.getEntry(iter.key()));
<CHANGEE>
}
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeMultiply(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() * v.getEntry(iter.key()));
}
/*
* MATH-803: the above loop assumes that 0d * x = 0d for any double x,
* which allows to consider only the non-zero entries of this. However,
* this fails if this[i] == 0d and (v[i] = NaN or v[i] = Infinity).
*
* These special cases are handled below.
*/
<CHANGES>
<CHANGEE>
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector getSubVector(int index, int n) {
checkIndex(index);
if (n < 0) {
throw new NotPositiveException(LocalizedFormats.NUMBER_OF_ELEMENTS_SHOULD_BE_POSITIVE, n);
}
checkIndex(index + n - 1);
OpenMapRealVector<SCANS>) {
throw new NullArgumentException();
}
data = new double[v.getDimension()];
for (int i = 0; i < data.length; ++i) {
data[i] = v.getEntry(i);
}
}
/**
* Construct a vector from another vector, using a deep copy.
*
* @param v Vector to copy.
* @throws NullArgumentException if {@code v} is {@code null}.
*/
public ArrayRealVector(ArrayRealVector v) {
this(v, true);
}
/**
* Construct a vector from another vector.
*
* @param v Vector to copy.
* @param deep If {@code true} perform a deep copy, otherwise perform a
* shallow copy.
*/
public ArrayRealVector(ArrayRealVector v, boolean deep) {
data = deep ? v.data.clone() : v.data;
}
/**
* Construct a vector by appending one vector to another vector.
* @param v1 First vector (will be put in front of the new vector).
* @param v2 Second vector (will be put at back of the new vector).
*/
public ArrayRealVector(ArrayRealVector v1, ArrayRealVector v2) {
data = new double[v1.data.length + v2.data.length];
System.arraycopy(v1.data, 0, data, 0, v1.data.length);
System.arraycopy(v2.data, 0, data, v1.data.length, v2.data.length);
}
/**
* Construct a vector by appending one vector to another vector.
* @param v1 First vector (will be put in front of the new vector).
* @param v2 Second vector (will be put at back of the new vector).
*/
public ArrayRealVector(ArrayRealVector v1, RealVector v2) {
final int l1 = v1.data.length;
final int l2 = v2.getDimension();
data = new double[l1 + l2];
System.arraycopy(v1.data, 0, data, 0, l1);
for (int i = 0; i < l2; ++i) {
data[l1 +
Math, 29
<FILEB>
<CHANGES>
final int n = getDimension();
for (int i = 0; i < n; i++) {
res.setEntry(i, this.getEntry(i) / v.getEntry(i));
<CHANGEE>
<CHANGES>
if (v.isNaN() || v.isInfinite()) {
final int n = getDimension();
for (int i = 0; i < n; i++) {
final double y = v.getEntry(i);
if (Double.isNaN(y)) {
res.setEntry(i, Double.NaN);
} else if (Double.isInfinite(y)) {
final double x = this.getEntry(i);
res.setEntry(i, x * y);
}
}
}
<CHANGEE>
<FILEE>
<FILEB>
@Override
public double dotProduct(RealVector v) {
if(v instanceof OpenMapRealVector) {
return dotProduct((OpenMapRealVector)v);
} else {
return super.dotProduct(v);
}
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeDivide(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
/*
* MATH-803: it is not sufficient to loop through non zero entries of
* this only. Indeed, if this[i] = 0d and v[i] = 0d, then
* this[i] / v[i] = NaN, and not 0d.
*/
<CHANGES>
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() / v.getEntry(iter.key()));
<CHANGEE>
}
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeMultiply(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() * v.getEntry(iter.key()));
}
/*
* MATH-803: the above loop assumes that 0d * x = 0d for any double x,
* which allows to consider only the non-zero entries of this. However,
* this fails if this[i] == 0d and (v[i] = NaN or v[i] = Infinity).
*
* These special cases are handled below.
*/
<CHANGES>
<CHANGEE>
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector getSubVector(int index, int n) {
checkIndex(index);
if (n < 0) {
throw new NotPositiveException(LocalizedFormats.NUMBER_OF_ELEMENTS_SHOULD_BE_POSITIVE, n);
}
checkIndex(index + n - 1);
OpenMapRealVector<SCANS> i] = v2.getEntry(i);
}
}
/**
* Construct a vector by appending one vector to another vector.
* @param v1 First vector (will be put in front of the new vector).
* @param v2 Second vector (will be put at back of the new vector).
*/
public ArrayRealVector(RealVector v1, ArrayRealVector v2) {
final int l1 = v1.getDimension();
final int l2 = v2.data.length;
data = new double[l1 + l2];
for (int i = 0; i < l1; ++i) {
data[i] = v1.getEntry(i);
}
System.arraycopy(v2.data, 0, data, l1, l2);
}
/**
* Construct a vector by appending one vector to another vector.
* @param v1 First vector (will be put in front of the new vector).
* @param v2 Second vector (will be put at back of the new vector).
*/
public ArrayRealVector(ArrayRealVector v1, double[] v2) {
final int l1 = v1.getDimension();
final int l2 = v2.length;
data = new double[l1 + l2];
System.arraycopy(v1.data, 0, data, 0, l1);
System.arraycopy(v2, 0, data, l1, l2);
}
/**
* Construct a vector by appending one vector to another vector.
* @param v1 First vector (will be put in front of the new vector).
* @param v2 Second vector (will be put at back of the new vector).
*/
public ArrayRealVector(double[] v1, ArrayRealVector v2) {
final int l1 = v1.length;
final int l2 = v2.getDimension();
data = new double[l1 + l2];
System.arraycopy(v1, 0, data, 0, l1);
System.arraycopy(v2.data, 0, data, l1, l2);
}
/**
* Construct a vector by appending one vector to another vector.
* @param v1 first vector (will
Math, 29
<FILEB>
<CHANGES>
final int n = getDimension();
for (int i = 0; i < n; i++) {
res.setEntry(i, this.getEntry(i) / v.getEntry(i));
<CHANGEE>
<CHANGES>
if (v.isNaN() || v.isInfinite()) {
final int n = getDimension();
for (int i = 0; i < n; i++) {
final double y = v.getEntry(i);
if (Double.isNaN(y)) {
res.setEntry(i, Double.NaN);
} else if (Double.isInfinite(y)) {
final double x = this.getEntry(i);
res.setEntry(i, x * y);
}
}
}
<CHANGEE>
<FILEE>
<FILEB>
@Override
public double dotProduct(RealVector v) {
if(v instanceof OpenMapRealVector) {
return dotProduct((OpenMapRealVector)v);
} else {
return super.dotProduct(v);
}
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeDivide(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
/*
* MATH-803: it is not sufficient to loop through non zero entries of
* this only. Indeed, if this[i] = 0d and v[i] = 0d, then
* this[i] / v[i] = NaN, and not 0d.
*/
<CHANGES>
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() / v.getEntry(iter.key()));
<CHANGEE>
}
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeMultiply(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() * v.getEntry(iter.key()));
}
/*
* MATH-803: the above loop assumes that 0d * x = 0d for any double x,
* which allows to consider only the non-zero entries of this. However,
* this fails if this[i] == 0d and (v[i] = NaN or v[i] = Infinity).
*
* These special cases are handled below.
*/
<CHANGES>
<CHANGEE>
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector getSubVector(int index, int n) {
checkIndex(index);
if (n < 0) {
throw new NotPositiveException(LocalizedFormats.NUMBER_OF_ELEMENTS_SHOULD_BE_POSITIVE, n);
}
checkIndex(index + n - 1);
OpenMapRealVector<SCANS> be put in front of the new vector)
* @param v2 second vector (will be put at back of the new vector)
*/
public ArrayRealVector(double[] v1, double[] v2) {
final int l1 = v1.length;
final int l2 = v2.length;
data = new double[l1 + l2];
System.arraycopy(v1, 0, data, 0, l1);
System.arraycopy(v2, 0, data, l1, l2);
}
/** {@inheritDoc} */
@Override
public ArrayRealVector copy() {
return new ArrayRealVector(this, true);
}
/** {@inheritDoc} */
@Override
public ArrayRealVector add(RealVector v) {
if (v instanceof ArrayRealVector) {
final double[] vData = ((ArrayRealVector) v).data;
final int dim = vData.length;
checkVectorDimensions(dim);
ArrayRealVector result = new ArrayRealVector(dim);
double[] resultData = result.data;
for (int i = 0; i < dim; i++) {
resultData[i] = data[i] + vData[i];
}
return result;
} else {
checkVectorDimensions(v);
double[] out = data.clone();
Iterator<Entry> it = v.sparseIterator();
while (it.hasNext()) {
final Entry e = it.next();
out[e.getIndex()] += e.getValue();
}
return new ArrayRealVector(out, false);
}
}
/** {@inheritDoc} */
@Override
public ArrayRealVector subtract(RealVector v) {
if (v instanceof ArrayRealVector) {
final double[] vData = ((ArrayRealVector) v).data;
final int dim = vData.length;
checkVectorDimensions(dim);
ArrayRealVector result = new ArrayRealVector(dim);
double[] resultData = result.data;
for (int i = 0; i < dim; i++) {
resultData[i] = data[i] - vData[i];
}
return result;
} else {
checkVectorDimensions(v);
double[] out = data.clone();
Iterator<Entry> it = v
Math, 29
<FILEB>
<CHANGES>
final int n = getDimension();
for (int i = 0; i < n; i++) {
res.setEntry(i, this.getEntry(i) / v.getEntry(i));
<CHANGEE>
<CHANGES>
if (v.isNaN() || v.isInfinite()) {
final int n = getDimension();
for (int i = 0; i < n; i++) {
final double y = v.getEntry(i);
if (Double.isNaN(y)) {
res.setEntry(i, Double.NaN);
} else if (Double.isInfinite(y)) {
final double x = this.getEntry(i);
res.setEntry(i, x * y);
}
}
}
<CHANGEE>
<FILEE>
<FILEB>
@Override
public double dotProduct(RealVector v) {
if(v instanceof OpenMapRealVector) {
return dotProduct((OpenMapRealVector)v);
} else {
return super.dotProduct(v);
}
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeDivide(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
/*
* MATH-803: it is not sufficient to loop through non zero entries of
* this only. Indeed, if this[i] = 0d and v[i] = 0d, then
* this[i] / v[i] = NaN, and not 0d.
*/
<CHANGES>
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() / v.getEntry(iter.key()));
<CHANGEE>
}
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeMultiply(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() * v.getEntry(iter.key()));
}
/*
* MATH-803: the above loop assumes that 0d * x = 0d for any double x,
* which allows to consider only the non-zero entries of this. However,
* this fails if this[i] == 0d and (v[i] = NaN or v[i] = Infinity).
*
* These special cases are handled below.
*/
<CHANGES>
<CHANGEE>
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector getSubVector(int index, int n) {
checkIndex(index);
if (n < 0) {
throw new NotPositiveException(LocalizedFormats.NUMBER_OF_ELEMENTS_SHOULD_BE_POSITIVE, n);
}
checkIndex(index + n - 1);
OpenMapRealVector<SCANS>.sparseIterator();
while (it.hasNext()) {
final Entry e = it.next();
out[e.getIndex()] -= e.getValue();
}
return new ArrayRealVector(out, false);
}
}
/** {@inheritDoc} */
@Override
public ArrayRealVector map(UnivariateFunction function) {
return copy().mapToSelf(function);
}
/** {@inheritDoc} */
@Override
public ArrayRealVector mapToSelf(UnivariateFunction function) {
for (int i = 0; i < data.length; i++) {
data[i] = function.value(data[i]);
}
return this;
}
/** {@inheritDoc} */
@Override
public RealVector mapAddToSelf(double d) {
for (int i = 0; i < data.length; i++) {
data[i] = data[i] + d;
}
return this;
}
/** {@inheritDoc} */
@Override
public RealVector mapSubtractToSelf(double d) {
for (int i = 0; i < data.length; i++) {
data[i] = data[i] - d;
}
return this;
}
/** {@inheritDoc} */
@Override
public RealVector mapMultiplyToSelf(double d) {
for (int i = 0; i < data.length; i++) {
data[i] = data[i] * d;
}
return this;
}
/** {@inheritDoc} */
@Override
public RealVector mapDivideToSelf(double d) {
for (int i = 0; i < data.length; i++) {
data[i] = data[i] / d;
}
return this;
}
/** {@inheritDoc} */
@Override
public ArrayRealVector ebeMultiply(RealVector v) {
if (v instanceof ArrayRealVector) {
final double[] vData = ((ArrayRealVector) v).data;
final int dim = vData.length;
checkVectorDimensions(dim);
ArrayRealVector result = new ArrayRealVector(dim);
double[] resultData = result.data;
for (int i = 0; i < dim; i++) {
resultData[i] = data[i] * v
Math, 29
<FILEB>
<CHANGES>
final int n = getDimension();
for (int i = 0; i < n; i++) {
res.setEntry(i, this.getEntry(i) / v.getEntry(i));
<CHANGEE>
<CHANGES>
if (v.isNaN() || v.isInfinite()) {
final int n = getDimension();
for (int i = 0; i < n; i++) {
final double y = v.getEntry(i);
if (Double.isNaN(y)) {
res.setEntry(i, Double.NaN);
} else if (Double.isInfinite(y)) {
final double x = this.getEntry(i);
res.setEntry(i, x * y);
}
}
}
<CHANGEE>
<FILEE>
<FILEB>
@Override
public double dotProduct(RealVector v) {
if(v instanceof OpenMapRealVector) {
return dotProduct((OpenMapRealVector)v);
} else {
return super.dotProduct(v);
}
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeDivide(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
/*
* MATH-803: it is not sufficient to loop through non zero entries of
* this only. Indeed, if this[i] = 0d and v[i] = 0d, then
* this[i] / v[i] = NaN, and not 0d.
*/
<CHANGES>
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() / v.getEntry(iter.key()));
<CHANGEE>
}
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeMultiply(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() * v.getEntry(iter.key()));
}
/*
* MATH-803: the above loop assumes that 0d * x = 0d for any double x,
* which allows to consider only the non-zero entries of this. However,
* this fails if this[i] == 0d and (v[i] = NaN or v[i] = Infinity).
*
* These special cases are handled below.
*/
<CHANGES>
<CHANGEE>
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector getSubVector(int index, int n) {
checkIndex(index);
if (n < 0) {
throw new NotPositiveException(LocalizedFormats.NUMBER_OF_ELEMENTS_SHOULD_BE_POSITIVE, n);
}
checkIndex(index + n - 1);
OpenMapRealVector<SCANS>Data[i];
}
return result;
} else {
checkVectorDimensions(v);
double[] out = data.clone();
for (int i = 0; i < data.length; i++) {
out[i] *= v.getEntry(i);
}
return new ArrayRealVector(out, false);
}
}
/** {@inheritDoc} */
@Override
public ArrayRealVector ebeDivide(RealVector v) {
if (v instanceof ArrayRealVector) {
final double[] vData = ((ArrayRealVector) v).data;
final int dim = vData.length;
checkVectorDimensions(dim);
ArrayRealVector result = new ArrayRealVector(dim);
double[] resultData = result.data;
for (int i = 0; i < dim; i++) {
resultData[i] = data[i] / vData[i];
}
return result;
} else {
checkVectorDimensions(v);
double[] out = data.clone();
for (int i = 0; i < data.length; i++) {
out[i] /= v.getEntry(i);
}
return new ArrayRealVector(out, false);
}
}
/**
* Get a reference to the underlying data array.
* This method does not make a fresh copy of the underlying data.
*
* @return the array of entries.
*/
public double[] getDataRef() {
return data;
}
/** {@inheritDoc} */
@Override
public double dotProduct(RealVector v) {
if (v instanceof ArrayRealVector) {
final double[] vData = ((ArrayRealVector) v).data;
checkVectorDimensions(vData.length);
double dot = 0;
for (int i = 0; i < data.length; i++) {
dot += data[i] * vData[i];
}
return dot;
} else {
checkVectorDimensions(v);
double dot = 0;
Iterator<Entry> it = v.sparseIterator();
while (it.hasNext()) {
final Entry e = it.next();
dot += data[e.getIndex()] * e.getValue();
}
return dot;
}
}
/** {@inheritDoc} */
@Override
public
Math, 29
<FILEB>
<CHANGES>
final int n = getDimension();
for (int i = 0; i < n; i++) {
res.setEntry(i, this.getEntry(i) / v.getEntry(i));
<CHANGEE>
<CHANGES>
if (v.isNaN() || v.isInfinite()) {
final int n = getDimension();
for (int i = 0; i < n; i++) {
final double y = v.getEntry(i);
if (Double.isNaN(y)) {
res.setEntry(i, Double.NaN);
} else if (Double.isInfinite(y)) {
final double x = this.getEntry(i);
res.setEntry(i, x * y);
}
}
}
<CHANGEE>
<FILEE>
<FILEB>
@Override
public double dotProduct(RealVector v) {
if(v instanceof OpenMapRealVector) {
return dotProduct((OpenMapRealVector)v);
} else {
return super.dotProduct(v);
}
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeDivide(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
/*
* MATH-803: it is not sufficient to loop through non zero entries of
* this only. Indeed, if this[i] = 0d and v[i] = 0d, then
* this[i] / v[i] = NaN, and not 0d.
*/
<CHANGES>
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() / v.getEntry(iter.key()));
<CHANGEE>
}
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeMultiply(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() * v.getEntry(iter.key()));
}
/*
* MATH-803: the above loop assumes that 0d * x = 0d for any double x,
* which allows to consider only the non-zero entries of this. However,
* this fails if this[i] == 0d and (v[i] = NaN or v[i] = Infinity).
*
* These special cases are handled below.
*/
<CHANGES>
<CHANGEE>
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector getSubVector(int index, int n) {
checkIndex(index);
if (n < 0) {
throw new NotPositiveException(LocalizedFormats.NUMBER_OF_ELEMENTS_SHOULD_BE_POSITIVE, n);
}
checkIndex(index + n - 1);
OpenMapRealVector<SCANS> double getNorm() {
double sum = 0;
for (double a : data) {
sum += a * a;
}
return FastMath.sqrt(sum);
}
/** {@inheritDoc} */
@Override
public double getL1Norm() {
double sum = 0;
for (double a : data) {
sum += FastMath.abs(a);
}
return sum;
}
/** {@inheritDoc} */
@Override
public double getLInfNorm() {
double max = 0;
for (double a : data) {
max = FastMath.max(max, FastMath.abs(a));
}
return max;
}
/** {@inheritDoc} */
@Override
public double getDistance(RealVector v) {
if (v instanceof ArrayRealVector) {
final double[] vData = ((ArrayRealVector) v).data;
checkVectorDimensions(vData.length);
double sum = 0;
for (int i = 0; i < data.length; ++i) {
final double delta = data[i] - vData[i];
sum += delta * delta;
}
return FastMath.sqrt(sum);
} else {
checkVectorDimensions(v);
double sum = 0;
for (int i = 0; i < data.length; ++i) {
final double delta = data[i] - v.getEntry(i);
sum += delta * delta;
}
return FastMath.sqrt(sum);
}
}
/** {@inheritDoc} */
@Override
public double getL1Distance(RealVector v) {
if (v instanceof ArrayRealVector) {
final double[] vData = ((ArrayRealVector) v).data;
checkVectorDimensions(vData.length);
double sum = 0;
for (int i = 0; i < data.length; ++i) {
final double delta = data[i] - vData[i];
sum += FastMath.abs(delta);
}
return sum;
} else {
checkVectorDimensions(v);
double sum = 0;
for (int i = 0; i < data.length; ++i) {
final double delta = data[i] - v.getEntry(i
Math, 29
<FILEB>
<CHANGES>
final int n = getDimension();
for (int i = 0; i < n; i++) {
res.setEntry(i, this.getEntry(i) / v.getEntry(i));
<CHANGEE>
<CHANGES>
if (v.isNaN() || v.isInfinite()) {
final int n = getDimension();
for (int i = 0; i < n; i++) {
final double y = v.getEntry(i);
if (Double.isNaN(y)) {
res.setEntry(i, Double.NaN);
} else if (Double.isInfinite(y)) {
final double x = this.getEntry(i);
res.setEntry(i, x * y);
}
}
}
<CHANGEE>
<FILEE>
<FILEB>
@Override
public double dotProduct(RealVector v) {
if(v instanceof OpenMapRealVector) {
return dotProduct((OpenMapRealVector)v);
} else {
return super.dotProduct(v);
}
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeDivide(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
/*
* MATH-803: it is not sufficient to loop through non zero entries of
* this only. Indeed, if this[i] = 0d and v[i] = 0d, then
* this[i] / v[i] = NaN, and not 0d.
*/
<CHANGES>
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() / v.getEntry(iter.key()));
<CHANGEE>
}
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeMultiply(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() * v.getEntry(iter.key()));
}
/*
* MATH-803: the above loop assumes that 0d * x = 0d for any double x,
* which allows to consider only the non-zero entries of this. However,
* this fails if this[i] == 0d and (v[i] = NaN or v[i] = Infinity).
*
* These special cases are handled below.
*/
<CHANGES>
<CHANGEE>
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector getSubVector(int index, int n) {
checkIndex(index);
if (n < 0) {
throw new NotPositiveException(LocalizedFormats.NUMBER_OF_ELEMENTS_SHOULD_BE_POSITIVE, n);
}
checkIndex(index + n - 1);
OpenMapRealVector<SCANS>);
sum += FastMath.abs(delta);
}
return sum;
}
}
/** {@inheritDoc} */
@Override
public double getLInfDistance(RealVector v) {
if (v instanceof ArrayRealVector) {
final double[] vData = ((ArrayRealVector) v).data;
checkVectorDimensions(vData.length);
double max = 0;
for (int i = 0; i < data.length; ++i) {
final double delta = data[i] - vData[i];
max = FastMath.max(max, FastMath.abs(delta));
}
return max;
} else {
checkVectorDimensions(v);
double max = 0;
for (int i = 0; i < data.length; ++i) {
final double delta = data[i] - v.getEntry(i);
max = FastMath.max(max, FastMath.abs(delta));
}
return max;
}
}
/** {@inheritDoc} */
@Override
public RealVector unitVector() {
final double norm = getNorm();
if (norm == 0) {
throw new MathArithmeticException(LocalizedFormats.ZERO_NORM);
}
return mapDivide(norm);
}
/** {@inheritDoc} */
@Override
public void unitize() {
final double norm = getNorm();
if (norm == 0) {
throw new MathArithmeticException(LocalizedFormats.ZERO_NORM);
}
mapDivideToSelf(norm);
}
/** {@inheritDoc} */
@Override
public RealVector projection(RealVector v) {
return v.mapMultiply(dotProduct(v) / v.dotProduct(v));
}
/** {@inheritDoc} */
@Override
public RealMatrix outerProduct(RealVector v) {
if (v instanceof ArrayRealVector) {
final double[] vData = ((ArrayRealVector) v).data;
final int m = data.length;
final int n = vData.length;
final RealMatrix out = MatrixUtils.createRealMatrix(m, n);
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
out.setEntry(i, j, data
Math, 29
<FILEB>
<CHANGES>
final int n = getDimension();
for (int i = 0; i < n; i++) {
res.setEntry(i, this.getEntry(i) / v.getEntry(i));
<CHANGEE>
<CHANGES>
if (v.isNaN() || v.isInfinite()) {
final int n = getDimension();
for (int i = 0; i < n; i++) {
final double y = v.getEntry(i);
if (Double.isNaN(y)) {
res.setEntry(i, Double.NaN);
} else if (Double.isInfinite(y)) {
final double x = this.getEntry(i);
res.setEntry(i, x * y);
}
}
}
<CHANGEE>
<FILEE>
<FILEB>
@Override
public double dotProduct(RealVector v) {
if(v instanceof OpenMapRealVector) {
return dotProduct((OpenMapRealVector)v);
} else {
return super.dotProduct(v);
}
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeDivide(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
/*
* MATH-803: it is not sufficient to loop through non zero entries of
* this only. Indeed, if this[i] = 0d and v[i] = 0d, then
* this[i] / v[i] = NaN, and not 0d.
*/
<CHANGES>
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() / v.getEntry(iter.key()));
<CHANGEE>
}
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeMultiply(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() * v.getEntry(iter.key()));
}
/*
* MATH-803: the above loop assumes that 0d * x = 0d for any double x,
* which allows to consider only the non-zero entries of this. However,
* this fails if this[i] == 0d and (v[i] = NaN or v[i] = Infinity).
*
* These special cases are handled below.
*/
<CHANGES>
<CHANGEE>
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector getSubVector(int index, int n) {
checkIndex(index);
if (n < 0) {
throw new NotPositiveException(LocalizedFormats.NUMBER_OF_ELEMENTS_SHOULD_BE_POSITIVE, n);
}
checkIndex(index + n - 1);
OpenMapRealVector<SCANS>[i] * vData[j]);
}
}
return out;
} else {
final int m = data.length;
final int n = v.getDimension();
final RealMatrix out = MatrixUtils.createRealMatrix(m, n);
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
out.setEntry(i, j, data[i] * v.getEntry(j));
}
}
return out;
}
}
/** {@inheritDoc} */
@Override
public double getEntry(int index) {
try {
return data[index];
} catch (IndexOutOfBoundsException e) {
throw new OutOfRangeException(LocalizedFormats.INDEX, index, 0,
getDimension() - 1);
}
}
/** {@inheritDoc} */
@Override
public int getDimension() {
return data.length;
}
/** {@inheritDoc} */
@Override
public RealVector append(RealVector v) {
try {
return new ArrayRealVector(this, (ArrayRealVector) v);
} catch (ClassCastException cce) {
return new ArrayRealVector(this, v);
}
}
/**
* Construct a vector by appending a vector to this vector.
*
* @param v Vector to append to this one.
* @return a new vector.
*/
public ArrayRealVector append(ArrayRealVector v) {
return new ArrayRealVector(this, v);
}
/** {@inheritDoc} */
@Override
public RealVector append(double in) {
final double[] out = new double[data.length + 1];
System.arraycopy(data, 0, out, 0, data.length);
out[data.length] = in;
return new ArrayRealVector(out, false);
}
/** {@inheritDoc} */
@Override
public RealVector getSubVector(int index, int n) {
if (n < 0) {
throw new NotPositiveException(LocalizedFormats.NUMBER_OF_ELEMENTS_SHOULD_BE_POSITIVE, n);
}
ArrayRealVector out = new ArrayRealVector(n);
try {
System.arraycopy(data, index, out.data
Math, 29
<FILEB>
<CHANGES>
final int n = getDimension();
for (int i = 0; i < n; i++) {
res.setEntry(i, this.getEntry(i) / v.getEntry(i));
<CHANGEE>
<CHANGES>
if (v.isNaN() || v.isInfinite()) {
final int n = getDimension();
for (int i = 0; i < n; i++) {
final double y = v.getEntry(i);
if (Double.isNaN(y)) {
res.setEntry(i, Double.NaN);
} else if (Double.isInfinite(y)) {
final double x = this.getEntry(i);
res.setEntry(i, x * y);
}
}
}
<CHANGEE>
<FILEE>
<FILEB>
@Override
public double dotProduct(RealVector v) {
if(v instanceof OpenMapRealVector) {
return dotProduct((OpenMapRealVector)v);
} else {
return super.dotProduct(v);
}
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeDivide(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
/*
* MATH-803: it is not sufficient to loop through non zero entries of
* this only. Indeed, if this[i] = 0d and v[i] = 0d, then
* this[i] / v[i] = NaN, and not 0d.
*/
<CHANGES>
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() / v.getEntry(iter.key()));
<CHANGEE>
}
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeMultiply(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() * v.getEntry(iter.key()));
}
/*
* MATH-803: the above loop assumes that 0d * x = 0d for any double x,
* which allows to consider only the non-zero entries of this. However,
* this fails if this[i] == 0d and (v[i] = NaN or v[i] = Infinity).
*
* These special cases are handled below.
*/
<CHANGES>
<CHANGEE>
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector getSubVector(int index, int n) {
checkIndex(index);
if (n < 0) {
throw new NotPositiveException(LocalizedFormats.NUMBER_OF_ELEMENTS_SHOULD_BE_POSITIVE, n);
}
checkIndex(index + n - 1);
OpenMapRealVector<SCANS>, 0, n);
} catch (IndexOutOfBoundsException e) {
checkIndex(index);
checkIndex(index + n - 1);
}
return out;
}
/** {@inheritDoc} */
@Override
public void setEntry(int index, double value) {
try {
data[index] = value;
} catch (IndexOutOfBoundsException e) {
checkIndex(index);
}
}
/** {@inheritDoc} */
@Override
public void addToEntry(int index, double increment) {
try {
data[index] += increment;
} catch(IndexOutOfBoundsException e){
throw new OutOfRangeException(LocalizedFormats.INDEX,
index, 0, data.length - 1);
}
}
/** {@inheritDoc} */
@Override
public void setSubVector(int index, RealVector v) {
if (v instanceof ArrayRealVector) {
setSubVector(index, ((ArrayRealVector) v).data);
} else {
try {
for (int i = index; i < index + v.getDimension(); ++i) {
data[i] = v.getEntry(i - index);
}
} catch (IndexOutOfBoundsException e) {
checkIndex(index);
checkIndex(index + v.getDimension() - 1);
}
}
}
/**
* Set a set of consecutive elements.
*
* @param index Index of first element to be set.
* @param v Vector containing the values to set.
* @throws org.apache.commons.math3.exception.OutOfRangeException
* if the index is inconsistent with the vector size.
*/
public void setSubVector(int index, double[] v) {
try {
System.arraycopy(v, 0, data, index, v.length);
} catch (IndexOutOfBoundsException e) {
checkIndex(index);
checkIndex(index + v.length - 1);
}
}
/** {@inheritDoc} */
@Override
public void set(double value) {
Arrays.fill(data, value);
}
/** {@inheritDoc} */
@Override
public double[] toArray(){
return data.clone();
}
/** {@inheritDoc} */
@Override
public String toString(){
return DEFAULT_FORMAT.format(this);
}
Math, 29
<FILEB>
<CHANGES>
final int n = getDimension();
for (int i = 0; i < n; i++) {
res.setEntry(i, this.getEntry(i) / v.getEntry(i));
<CHANGEE>
<CHANGES>
if (v.isNaN() || v.isInfinite()) {
final int n = getDimension();
for (int i = 0; i < n; i++) {
final double y = v.getEntry(i);
if (Double.isNaN(y)) {
res.setEntry(i, Double.NaN);
} else if (Double.isInfinite(y)) {
final double x = this.getEntry(i);
res.setEntry(i, x * y);
}
}
}
<CHANGEE>
<FILEE>
<FILEB>
@Override
public double dotProduct(RealVector v) {
if(v instanceof OpenMapRealVector) {
return dotProduct((OpenMapRealVector)v);
} else {
return super.dotProduct(v);
}
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeDivide(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
/*
* MATH-803: it is not sufficient to loop through non zero entries of
* this only. Indeed, if this[i] = 0d and v[i] = 0d, then
* this[i] / v[i] = NaN, and not 0d.
*/
<CHANGES>
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() / v.getEntry(iter.key()));
<CHANGEE>
}
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeMultiply(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() * v.getEntry(iter.key()));
}
/*
* MATH-803: the above loop assumes that 0d * x = 0d for any double x,
* which allows to consider only the non-zero entries of this. However,
* this fails if this[i] == 0d and (v[i] = NaN or v[i] = Infinity).
*
* These special cases are handled below.
*/
<CHANGES>
<CHANGEE>
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector getSubVector(int index, int n) {
checkIndex(index);
if (n < 0) {
throw new NotPositiveException(LocalizedFormats.NUMBER_OF_ELEMENTS_SHOULD_BE_POSITIVE, n);
}
checkIndex(index + n - 1);
OpenMapRealVector<SCANS> /**
* Check if instance and specified vectors have the same dimension.
*
* @param v Vector to compare instance with.
* @throws DimensionMismatchException if the vectors do not
* have the same dimension.
*/
@Override
protected void checkVectorDimensions(RealVector v) {
checkVectorDimensions(v.getDimension());
}
/**
* Check if instance dimension is equal to some expected value.
*
* @param n Expected dimension.
* @throws DimensionMismatchException if the dimension is
* inconsistent with vector size.
*/
@Override
protected void checkVectorDimensions(int n) {
if (data.length != n) {
throw new DimensionMismatchException(data.length, n);
}
}
/**
* Check if any coordinate of this vector is {@code NaN}.
*
* @return {@code true} if any coordinate of this vector is {@code NaN},
* {@code false} otherwise.
*/
@Override
public boolean isNaN() {
for (double v : data) {
if (Double.isNaN(v)) {
return true;
}
}
return false;
}
/**
* Check whether any coordinate of this vector is infinite and none
* are {@code NaN}.
*
* @return {@code true} if any coordinate of this vector is infinite and
* none are {@code NaN}, {@code false} otherwise.
*/
@Override
public boolean isInfinite() {
if (isNaN()) {
return false;
}
for (double v : data) {
if (Double.isInfinite(v)) {
return true;
}
}
return false;
}
/**
* Test for the equality of two real vectors.
* If all coordinates of two real vectors are exactly the same, and none are
* {@code NaN}, the two real vectors are considered to be equal.
* {@code NaN} coordinates are considered to affect globally the vector
* and be equals to each other - i.e, if either (or all) coordinates of the
* real vector are equal to {@code NaN}, the real vector is equal to
* a vector with all {@code NaN} coordinates.
*
* @param other Object to test for equality.
* @return {@code true} if two vector objects are equal, {@code false} if
Math, 29
<FILEB>
<CHANGES>
final int n = getDimension();
for (int i = 0; i < n; i++) {
res.setEntry(i, this.getEntry(i) / v.getEntry(i));
<CHANGEE>
<CHANGES>
if (v.isNaN() || v.isInfinite()) {
final int n = getDimension();
for (int i = 0; i < n; i++) {
final double y = v.getEntry(i);
if (Double.isNaN(y)) {
res.setEntry(i, Double.NaN);
} else if (Double.isInfinite(y)) {
final double x = this.getEntry(i);
res.setEntry(i, x * y);
}
}
}
<CHANGEE>
<FILEE>
<FILEB>
@Override
public double dotProduct(RealVector v) {
if(v instanceof OpenMapRealVector) {
return dotProduct((OpenMapRealVector)v);
} else {
return super.dotProduct(v);
}
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeDivide(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
/*
* MATH-803: it is not sufficient to loop through non zero entries of
* this only. Indeed, if this[i] = 0d and v[i] = 0d, then
* this[i] / v[i] = NaN, and not 0d.
*/
<CHANGES>
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() / v.getEntry(iter.key()));
<CHANGEE>
}
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeMultiply(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() * v.getEntry(iter.key()));
}
/*
* MATH-803: the above loop assumes that 0d * x = 0d for any double x,
* which allows to consider only the non-zero entries of this. However,
* this fails if this[i] == 0d and (v[i] = NaN or v[i] = Infinity).
*
* These special cases are handled below.
*/
<CHANGES>
<CHANGEE>
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector getSubVector(int index, int n) {
checkIndex(index);
if (n < 0) {
throw new NotPositiveException(LocalizedFormats.NUMBER_OF_ELEMENTS_SHOULD_BE_POSITIVE, n);
}
checkIndex(index + n - 1);
OpenMapRealVector<SCANS> * {@code other} is null, not an instance of {@code RealVector}, or
* not equal to this {@code RealVector} instance.
*/
@Override
public boolean equals(Object other) {
if (this == other) {
return true;
}
if (!(other instanceof RealVector)) {
return false;
}
RealVector rhs = (RealVector) other;
if (data.length != rhs.getDimension()) {
return false;
}
if (rhs.isNaN()) {
return this.isNaN();
}
for (int i = 0; i < data.length; ++i) {
if (data[i] != rhs.getEntry(i)) {
return false;
}
}
return true;
}
/**
* Get a hashCode for the real vector.
* All {@code NaN} values have the same hash code.
*
* @return a hash code.
*/
@Override
public int hashCode() {
if (isNaN()) {
return 9;
}
return MathUtils.hash(data);
}
/** {@inheritDoc} */
@Override
public ArrayRealVector combine(double a, double b, RealVector y) {
return copy().combineToSelf(a, b, y);
}
/** {@inheritDoc} */
@Override
public ArrayRealVector combineToSelf(double a, double b, RealVector y) {
if (y instanceof ArrayRealVector) {
final double[] yData = ((ArrayRealVector) y).data;
checkVectorDimensions(yData.length);
for (int i = 0; i < this.data.length; i++) {
data[i] = a * data[i] + b * yData[i];
}
} else {
checkVectorDimensions(y);
for (int i = 0; i < this.data.length; i++) {
data[i] = a * data[i] + b * y.getEntry(i);
}
}
return this;
}
/** {@inheritDoc} */
@Override
public double walkInDefaultOrder(final RealVectorPreservingVisitor visitor) {
visitor.start(data.length, 0, data.length - 1);
for (int i = 0; i < data.
Math, 29
<FILEB>
<CHANGES>
final int n = getDimension();
for (int i = 0; i < n; i++) {
res.setEntry(i, this.getEntry(i) / v.getEntry(i));
<CHANGEE>
<CHANGES>
if (v.isNaN() || v.isInfinite()) {
final int n = getDimension();
for (int i = 0; i < n; i++) {
final double y = v.getEntry(i);
if (Double.isNaN(y)) {
res.setEntry(i, Double.NaN);
} else if (Double.isInfinite(y)) {
final double x = this.getEntry(i);
res.setEntry(i, x * y);
}
}
}
<CHANGEE>
<FILEE>
<FILEB>
@Override
public double dotProduct(RealVector v) {
if(v instanceof OpenMapRealVector) {
return dotProduct((OpenMapRealVector)v);
} else {
return super.dotProduct(v);
}
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeDivide(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
/*
* MATH-803: it is not sufficient to loop through non zero entries of
* this only. Indeed, if this[i] = 0d and v[i] = 0d, then
* this[i] / v[i] = NaN, and not 0d.
*/
<CHANGES>
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() / v.getEntry(iter.key()));
<CHANGEE>
}
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeMultiply(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() * v.getEntry(iter.key()));
}
/*
* MATH-803: the above loop assumes that 0d * x = 0d for any double x,
* which allows to consider only the non-zero entries of this. However,
* this fails if this[i] == 0d and (v[i] = NaN or v[i] = Infinity).
*
* These special cases are handled below.
*/
<CHANGES>
<CHANGEE>
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector getSubVector(int index, int n) {
checkIndex(index);
if (n < 0) {
throw new NotPositiveException(LocalizedFormats.NUMBER_OF_ELEMENTS_SHOULD_BE_POSITIVE, n);
}
checkIndex(index + n - 1);
OpenMapRealVector<SCANS>length; i++) {
visitor.visit(i, data[i]);
}
return visitor.end();
}
/** {@inheritDoc} */
@Override
public double walkInDefaultOrder(final RealVectorPreservingVisitor visitor,
final int start, final int end) {
checkIndices(start, end);
visitor.start(data.length, start, end);
for (int i = start; i <= end; i++) {
visitor.visit(i, data[i]);
}
return visitor.end();
}
/**
* {@inheritDoc}
*
* In this implementation, the optimized order is the default order.
*/
@Override
public double walkInOptimizedOrder(final RealVectorPreservingVisitor visitor) {
return walkInDefaultOrder(visitor);
}
/**
* {@inheritDoc}
*
* In this implementation, the optimized order is the default order.
*/
@Override
public double walkInOptimizedOrder(final RealVectorPreservingVisitor visitor,
final int start, final int end) {
return walkInDefaultOrder(visitor, start, end);
}
/** {@inheritDoc} */
@Override
public double walkInDefaultOrder(final RealVectorChangingVisitor visitor) {
visitor.start(data.length, 0, data.length - 1);
for (int i = 0; i < data.length; i++) {
data[i] = visitor.visit(i, data[i]);
}
return visitor.end();
}
/** {@inheritDoc} */
@Override
public double walkInDefaultOrder(final RealVectorChangingVisitor visitor,
final int start, final int end) {
checkIndices(start, end);
visitor.start(data.length, start, end);
for (int i = start; i <= end; i++) {
data[i] = visitor.visit(i, data[i]);
}
return visitor.end();
}
/**
* {@inheritDoc}
*
* In this implementation, the optimized order is the default order.
*/
@Override
public double walkInOptimizedOrder(final RealVectorChangingVisitor visitor) {
return walkInDefaultOrder(visitor);
}
/**
* {@inheritDoc}
*
* In this implementation, the optimized order is the default order.
Math, 29
<FILEB>
<CHANGES>
final int n = getDimension();
for (int i = 0; i < n; i++) {
res.setEntry(i, this.getEntry(i) / v.getEntry(i));
<CHANGEE>
<CHANGES>
if (v.isNaN() || v.isInfinite()) {
final int n = getDimension();
for (int i = 0; i < n; i++) {
final double y = v.getEntry(i);
if (Double.isNaN(y)) {
res.setEntry(i, Double.NaN);
} else if (Double.isInfinite(y)) {
final double x = this.getEntry(i);
res.setEntry(i, x * y);
}
}
}
<CHANGEE>
<FILEE>
<FILEB>
@Override
public double dotProduct(RealVector v) {
if(v instanceof OpenMapRealVector) {
return dotProduct((OpenMapRealVector)v);
} else {
return super.dotProduct(v);
}
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeDivide(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
/*
* MATH-803: it is not sufficient to loop through non zero entries of
* this only. Indeed, if this[i] = 0d and v[i] = 0d, then
* this[i] / v[i] = NaN, and not 0d.
*/
<CHANGES>
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() / v.getEntry(iter.key()));
<CHANGEE>
}
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeMultiply(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() * v.getEntry(iter.key()));
}
/*
* MATH-803: the above loop assumes that 0d * x = 0d for any double x,
* which allows to consider only the non-zero entries of this. However,
* this fails if this[i] == 0d and (v[i] = NaN or v[i] = Infinity).
*
* These special cases are handled below.
*/
<CHANGES>
<CHANGEE>
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector getSubVector(int index, int n) {
checkIndex(index);
if (n < 0) {
throw new NotPositiveException(LocalizedFormats.NUMBER_OF_ELEMENTS_SHOULD_BE_POSITIVE, n);
}
checkIndex(index + n - 1);
OpenMapRealVector<SCANS>;
// Multiply.
for (int col = 0; col < nCols; col++) {
// Copy all elements of column "col" of "m" so that
// will be in contiguous memory.
for (int mRow = 0; mRow < nSum; mRow++) {
mCol[mRow] = mData[mRow][col];
}
for (int row = 0; row < nRows; row++) {
final double[] dataRow = data[row];
double sum = 0;
for (int i = 0; i < nSum; i++) {
sum += dataRow[i] * mCol[i];
}
outData[row][col] = sum;
}
}
return new Array2DRowRealMatrix(outData, false);
}
/** {@inheritDoc} */
@Override
public double[][] getData() {
return copyOut();
}
/**
* Get a reference to the underlying data array.
*
* @return 2-dimensional array of entries.
*/
public double[][] getDataRef() {
return data;
}
/** {@inheritDoc} */
@Override
public void setSubMatrix(final double[][] subMatrix,
final int row, final int column) {
if (data == null) {
if (row > 0) {
throw new MathIllegalStateException(LocalizedFormats.FIRST_ROWS_NOT_INITIALIZED_YET, row);
}
if (column > 0) {
throw new MathIllegalStateException(LocalizedFormats.FIRST_COLUMNS_NOT_INITIALIZED_YET, column);
}
MathUtils.checkNotNull(subMatrix);
final int nRows = subMatrix.length;
if (nRows == 0) {
throw new NoDataException(LocalizedFormats.AT_LEAST_ONE_ROW);
}
final int nCols = subMatrix[0].length;
if (nCols == 0) {
throw new NoDataException(LocalizedFormats.AT_LEAST_ONE_COLUMN);
}
data = new double[subMatrix.length][nCols];
for (int i = 0; i < data.length; ++i) {
if (subMatrix[i].length != nCols) {
throw new DimensionMismatchException(subMatrix[i
Math, 29
<FILEB>
<CHANGES>
final int n = getDimension();
for (int i = 0; i < n; i++) {
res.setEntry(i, this.getEntry(i) / v.getEntry(i));
<CHANGEE>
<CHANGES>
if (v.isNaN() || v.isInfinite()) {
final int n = getDimension();
for (int i = 0; i < n; i++) {
final double y = v.getEntry(i);
if (Double.isNaN(y)) {
res.setEntry(i, Double.NaN);
} else if (Double.isInfinite(y)) {
final double x = this.getEntry(i);
res.setEntry(i, x * y);
}
}
}
<CHANGEE>
<FILEE>
<FILEB>
@Override
public double dotProduct(RealVector v) {
if(v instanceof OpenMapRealVector) {
return dotProduct((OpenMapRealVector)v);
} else {
return super.dotProduct(v);
}
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeDivide(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
/*
* MATH-803: it is not sufficient to loop through non zero entries of
* this only. Indeed, if this[i] = 0d and v[i] = 0d, then
* this[i] / v[i] = NaN, and not 0d.
*/
<CHANGES>
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() / v.getEntry(iter.key()));
<CHANGEE>
}
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeMultiply(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() * v.getEntry(iter.key()));
}
/*
* MATH-803: the above loop assumes that 0d * x = 0d for any double x,
* which allows to consider only the non-zero entries of this. However,
* this fails if this[i] == 0d and (v[i] = NaN or v[i] = Infinity).
*
* These special cases are handled below.
*/
<CHANGES>
<CHANGEE>
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector getSubVector(int index, int n) {
checkIndex(index);
if (n < 0) {
throw new NotPositiveException(LocalizedFormats.NUMBER_OF_ELEMENTS_SHOULD_BE_POSITIVE, n);
}
checkIndex(index + n - 1);
OpenMapRealVector<SCANS>].length, nCols);
}
System.arraycopy(subMatrix[i], 0, data[i + row], column, nCols);
}
} else {
super.setSubMatrix(subMatrix, row, column);
}
}
/** {@inheritDoc} */
@Override
public double getEntry(final int row, final int column) {
MatrixUtils.checkMatrixIndex(this, row, column);
return data[row][column];
}
/** {@inheritDoc} */
@Override
public void setEntry(final int row, final int column, final double value) {
MatrixUtils.checkMatrixIndex(this, row, column);
data[row][column] = value;
}
/** {@inheritDoc} */
@Override
public void addToEntry(final int row, final int column, final double increment) {
MatrixUtils.checkMatrixIndex(this, row, column);
data[row][column] += increment;
}
/** {@inheritDoc} */
@Override
public void multiplyEntry(final int row, final int column, final double factor) {
MatrixUtils.checkMatrixIndex(this, row, column);
data[row][column] *= factor;
}
/** {@inheritDoc} */
@Override
public int getRowDimension() {
return (data == null) ? 0 : data.length;
}
/** {@inheritDoc} */
@Override
public int getColumnDimension() {
return ((data == null) || (data[0] == null)) ? 0 : data[0].length;
}
/** {@inheritDoc} */
@Override
public double[] operate(final double[] v) {
final int nRows = this.getRowDimension();
final int nCols = this.getColumnDimension();
if (v.length != nCols) {
throw new DimensionMismatchException(v.length, nCols);
}
final double[] out = new double[nRows];
for (int row = 0; row < nRows; row++) {
final double[] dataRow = data[row];
double sum = 0;
for (int i = 0; i < nCols; i++) {
sum += dataRow[i] * v[i];
}
out[row] = sum;
}
return out;
Math, 29
<FILEB>
<CHANGES>
final int n = getDimension();
for (int i = 0; i < n; i++) {
res.setEntry(i, this.getEntry(i) / v.getEntry(i));
<CHANGEE>
<CHANGES>
if (v.isNaN() || v.isInfinite()) {
final int n = getDimension();
for (int i = 0; i < n; i++) {
final double y = v.getEntry(i);
if (Double.isNaN(y)) {
res.setEntry(i, Double.NaN);
} else if (Double.isInfinite(y)) {
final double x = this.getEntry(i);
res.setEntry(i, x * y);
}
}
}
<CHANGEE>
<FILEE>
<FILEB>
@Override
public double dotProduct(RealVector v) {
if(v instanceof OpenMapRealVector) {
return dotProduct((OpenMapRealVector)v);
} else {
return super.dotProduct(v);
}
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeDivide(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
/*
* MATH-803: it is not sufficient to loop through non zero entries of
* this only. Indeed, if this[i] = 0d and v[i] = 0d, then
* this[i] / v[i] = NaN, and not 0d.
*/
<CHANGES>
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() / v.getEntry(iter.key()));
<CHANGEE>
}
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeMultiply(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() * v.getEntry(iter.key()));
}
/*
* MATH-803: the above loop assumes that 0d * x = 0d for any double x,
* which allows to consider only the non-zero entries of this. However,
* this fails if this[i] == 0d and (v[i] = NaN or v[i] = Infinity).
*
* These special cases are handled below.
*/
<CHANGES>
<CHANGEE>
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector getSubVector(int index, int n) {
checkIndex(index);
if (n < 0) {
throw new NotPositiveException(LocalizedFormats.NUMBER_OF_ELEMENTS_SHOULD_BE_POSITIVE, n);
}
checkIndex(index + n - 1);
OpenMapRealVector<SCANS> }
/** {@inheritDoc} */
@Override
public double[] preMultiply(final double[] v) {
final int nRows = getRowDimension();
final int nCols = getColumnDimension();
if (v.length != nRows) {
throw new DimensionMismatchException(v.length, nRows);
}
final double[] out = new double[nCols];
for (int col = 0; col < nCols; ++col) {
double sum = 0;
for (int i = 0; i < nRows; ++i) {
sum += data[i][col] * v[i];
}
out[col] = sum;
}
return out;
}
/** {@inheritDoc} */
@Override
public double walkInRowOrder(final RealMatrixChangingVisitor visitor) {
final int rows = getRowDimension();
final int columns = getColumnDimension();
visitor.start(rows, columns, 0, rows - 1, 0, columns - 1);
for (int i = 0; i < rows; ++i) {
final double[] rowI = data[i];
for (int j = 0; j < columns; ++j) {
rowI[j] = visitor.visit(i, j, rowI[j]);
}
}
return visitor.end();
}
/** {@inheritDoc} */
@Override
public double walkInRowOrder(final RealMatrixPreservingVisitor visitor) {
final int rows = getRowDimension();
final int columns = getColumnDimension();
visitor.start(rows, columns, 0, rows - 1, 0, columns - 1);
for (int i = 0; i < rows; ++i) {
final double[] rowI = data[i];
for (int j = 0; j < columns; ++j) {
visitor.visit(i, j, rowI[j]);
}
}
return visitor.end();
}
/** {@inheritDoc} */
@Override
public double walkInRowOrder(final RealMatrixChangingVisitor visitor,
final int startRow, final int endRow,
final int startColumn, final int endColumn) {
MatrixUtils.checkSubMatrixIndex(this, startRow, endRow, start
Math, 29
<FILEB>
<CHANGES>
final int n = getDimension();
for (int i = 0; i < n; i++) {
res.setEntry(i, this.getEntry(i) / v.getEntry(i));
<CHANGEE>
<CHANGES>
if (v.isNaN() || v.isInfinite()) {
final int n = getDimension();
for (int i = 0; i < n; i++) {
final double y = v.getEntry(i);
if (Double.isNaN(y)) {
res.setEntry(i, Double.NaN);
} else if (Double.isInfinite(y)) {
final double x = this.getEntry(i);
res.setEntry(i, x * y);
}
}
}
<CHANGEE>
<FILEE>
<FILEB>
@Override
public double dotProduct(RealVector v) {
if(v instanceof OpenMapRealVector) {
return dotProduct((OpenMapRealVector)v);
} else {
return super.dotProduct(v);
}
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeDivide(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
/*
* MATH-803: it is not sufficient to loop through non zero entries of
* this only. Indeed, if this[i] = 0d and v[i] = 0d, then
* this[i] / v[i] = NaN, and not 0d.
*/
<CHANGES>
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() / v.getEntry(iter.key()));
<CHANGEE>
}
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeMultiply(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() * v.getEntry(iter.key()));
}
/*
* MATH-803: the above loop assumes that 0d * x = 0d for any double x,
* which allows to consider only the non-zero entries of this. However,
* this fails if this[i] == 0d and (v[i] = NaN or v[i] = Infinity).
*
* These special cases are handled below.
*/
<CHANGES>
<CHANGEE>
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector getSubVector(int index, int n) {
checkIndex(index);
if (n < 0) {
throw new NotPositiveException(LocalizedFormats.NUMBER_OF_ELEMENTS_SHOULD_BE_POSITIVE, n);
}
checkIndex(index + n - 1);
OpenMapRealVector<SCANS>Column, endColumn);
visitor.start(getRowDimension(), getColumnDimension(),
startRow, endRow, startColumn, endColumn);
for (int i = startRow; i <= endRow; ++i) {
final double[] rowI = data[i];
for (int j = startColumn; j <= endColumn; ++j) {
rowI[j] = visitor.visit(i, j, rowI[j]);
}
}
return visitor.end();
}
/** {@inheritDoc} */
@Override
public double walkInRowOrder(final RealMatrixPreservingVisitor visitor,
final int startRow, final int endRow,
final int startColumn, final int endColumn) {
MatrixUtils.checkSubMatrixIndex(this, startRow, endRow, startColumn, endColumn);
visitor.start(getRowDimension(), getColumnDimension(),
startRow, endRow, startColumn, endColumn);
for (int i = startRow; i <= endRow; ++i) {
final double[] rowI = data[i];
for (int j = startColumn; j <= endColumn; ++j) {
visitor.visit(i, j, rowI[j]);
}
}
return visitor.end();
}
/** {@inheritDoc} */
@Override
public double walkInColumnOrder(final RealMatrixChangingVisitor visitor) {
final int rows = getRowDimension();
final int columns = getColumnDimension();
visitor.start(rows, columns, 0, rows - 1, 0, columns - 1);
for (int j = 0; j < columns; ++j) {
for (int i = 0; i < rows; ++i) {
final double[] rowI = data[i];
rowI[j] = visitor.visit(i, j, rowI[j]);
}
}
return visitor.end();
}
/** {@inheritDoc} */
@Override
public double walkInColumnOrder(final RealMatrixPreservingVisitor visitor) {
final int rows = getRowDimension();
final int columns = getColumnDimension();
visitor.start(rows, columns, 0, rows - 1, 0, columns - 1);
for (int j = 0; j < columns; ++j
Math, 29
<FILEB>
<CHANGES>
final int n = getDimension();
for (int i = 0; i < n; i++) {
res.setEntry(i, this.getEntry(i) / v.getEntry(i));
<CHANGEE>
<CHANGES>
if (v.isNaN() || v.isInfinite()) {
final int n = getDimension();
for (int i = 0; i < n; i++) {
final double y = v.getEntry(i);
if (Double.isNaN(y)) {
res.setEntry(i, Double.NaN);
} else if (Double.isInfinite(y)) {
final double x = this.getEntry(i);
res.setEntry(i, x * y);
}
}
}
<CHANGEE>
<FILEE>
<FILEB>
@Override
public double dotProduct(RealVector v) {
if(v instanceof OpenMapRealVector) {
return dotProduct((OpenMapRealVector)v);
} else {
return super.dotProduct(v);
}
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeDivide(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
/*
* MATH-803: it is not sufficient to loop through non zero entries of
* this only. Indeed, if this[i] = 0d and v[i] = 0d, then
* this[i] / v[i] = NaN, and not 0d.
*/
<CHANGES>
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() / v.getEntry(iter.key()));
<CHANGEE>
}
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeMultiply(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() * v.getEntry(iter.key()));
}
/*
* MATH-803: the above loop assumes that 0d * x = 0d for any double x,
* which allows to consider only the non-zero entries of this. However,
* this fails if this[i] == 0d and (v[i] = NaN or v[i] = Infinity).
*
* These special cases are handled below.
*/
<CHANGES>
<CHANGEE>
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector getSubVector(int index, int n) {
checkIndex(index);
if (n < 0) {
throw new NotPositiveException(LocalizedFormats.NUMBER_OF_ELEMENTS_SHOULD_BE_POSITIVE, n);
}
checkIndex(index + n - 1);
OpenMapRealVector<SCANS>) {
for (int i = 0; i < rows; ++i) {
visitor.visit(i, j, data[i][j]);
}
}
return visitor.end();
}
/** {@inheritDoc} */
@Override
public double walkInColumnOrder(final RealMatrixChangingVisitor visitor,
final int startRow, final int endRow,
final int startColumn, final int endColumn) {
MatrixUtils.checkSubMatrixIndex(this, startRow, endRow, startColumn, endColumn);
visitor.start(getRowDimension(), getColumnDimension(),
startRow, endRow, startColumn, endColumn);
for (int j = startColumn; j <= endColumn; ++j) {
for (int i = startRow; i <= endRow; ++i) {
final double[] rowI = data[i];
rowI[j] = visitor.visit(i, j, rowI[j]);
}
}
return visitor.end();
}
/** {@inheritDoc} */
@Override
public double walkInColumnOrder(final RealMatrixPreservingVisitor visitor,
final int startRow, final int endRow,
final int startColumn, final int endColumn) {
MatrixUtils.checkSubMatrixIndex(this, startRow, endRow, startColumn, endColumn);
visitor.start(getRowDimension(), getColumnDimension(),
startRow, endRow, startColumn, endColumn);
for (int j = startColumn; j <= endColumn; ++j) {
for (int i = startRow; i <= endRow; ++i) {
visitor.visit(i, j, data[i][j]);
}
}
return visitor.end();
}
/**
* Get a fresh copy of the underlying data array.
*
* @return a copy of the underlying data array.
*/
private double[][] copyOut() {
final int nRows = this.getRowDimension();
final double[][] out = new double[nRows][this.getColumnDimension()];
// can't copy 2-d array in one shot, otherwise get row references
for (int i = 0; i < nRows; i++) {
System.arraycopy(data[i], 0, out[i], 0, data[i].length);
}
return
Math, 29
<FILEB>
<CHANGES>
final int n = getDimension();
for (int i = 0; i < n; i++) {
res.setEntry(i, this.getEntry(i) / v.getEntry(i));
<CHANGEE>
<CHANGES>
if (v.isNaN() || v.isInfinite()) {
final int n = getDimension();
for (int i = 0; i < n; i++) {
final double y = v.getEntry(i);
if (Double.isNaN(y)) {
res.setEntry(i, Double.NaN);
} else if (Double.isInfinite(y)) {
final double x = this.getEntry(i);
res.setEntry(i, x * y);
}
}
}
<CHANGEE>
<FILEE>
<FILEB>
@Override
public double dotProduct(RealVector v) {
if(v instanceof OpenMapRealVector) {
return dotProduct((OpenMapRealVector)v);
} else {
return super.dotProduct(v);
}
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeDivide(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
/*
* MATH-803: it is not sufficient to loop through non zero entries of
* this only. Indeed, if this[i] = 0d and v[i] = 0d, then
* this[i] / v[i] = NaN, and not 0d.
*/
<CHANGES>
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() / v.getEntry(iter.key()));
<CHANGEE>
}
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeMultiply(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() * v.getEntry(iter.key()));
}
/*
* MATH-803: the above loop assumes that 0d * x = 0d for any double x,
* which allows to consider only the non-zero entries of this. However,
* this fails if this[i] == 0d and (v[i] = NaN or v[i] = Infinity).
*
* These special cases are handled below.
*/
<CHANGES>
<CHANGEE>
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector getSubVector(int index, int n) {
checkIndex(index);
if (n < 0) {
throw new NotPositiveException(LocalizedFormats.NUMBER_OF_ELEMENTS_SHOULD_BE_POSITIVE, n);
}
checkIndex(index + n - 1);
OpenMapRealVector<SCANS>/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.math3.analysis;
/**
* An interface representing a bivariate real function.
*
* @since 2.1
* @version $Id$
*/
public interface BivariateFunction {
/**
* Compute the value for the function.
*
* @param x Abscissa for which the function value should be computed.
* @param y Ordinate for which the function value should be computed.
* @return the value.
*/
double value(double x, double y);
}
Math, 29
<FILEB>
<CHANGES>
final int n = getDimension();
for (int i = 0; i < n; i++) {
res.setEntry(i, this.getEntry(i) / v.getEntry(i));
<CHANGEE>
<CHANGES>
if (v.isNaN() || v.isInfinite()) {
final int n = getDimension();
for (int i = 0; i < n; i++) {
final double y = v.getEntry(i);
if (Double.isNaN(y)) {
res.setEntry(i, Double.NaN);
} else if (Double.isInfinite(y)) {
final double x = this.getEntry(i);
res.setEntry(i, x * y);
}
}
}
<CHANGEE>
<FILEE>
<FILEB>
@Override
public double dotProduct(RealVector v) {
if(v instanceof OpenMapRealVector) {
return dotProduct((OpenMapRealVector)v);
} else {
return super.dotProduct(v);
}
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeDivide(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
/*
* MATH-803: it is not sufficient to loop through non zero entries of
* this only. Indeed, if this[i] = 0d and v[i] = 0d, then
* this[i] / v[i] = NaN, and not 0d.
*/
<CHANGES>
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() / v.getEntry(iter.key()));
<CHANGEE>
}
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeMultiply(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() * v.getEntry(iter.key()));
}
/*
* MATH-803: the above loop assumes that 0d * x = 0d for any double x,
* which allows to consider only the non-zero entries of this. However,
* this fails if this[i] == 0d and (v[i] = NaN or v[i] = Infinity).
*
* These special cases are handled below.
*/
<CHANGES>
<CHANGEE>
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector getSubVector(int index, int n) {
checkIndex(index);
if (n < 0) {
throw new NotPositiveException(LocalizedFormats.NUMBER_OF_ELEMENTS_SHOULD_BE_POSITIVE, n);
}
checkIndex(index + n - 1);
OpenMapRealVector<SCANS> } catch (LocalException le) {
* // Retrieve the x value.
* }
* }
* </pre>
*
* As shown, the exception is local to the user's code and it is guaranteed
* that Apache Commons Math will not catch it.
*
* @version $Id$
*/
public interface UnivariateFunction {
/**
* Compute the value of the function.
*
* @param x Point at which the function value should be computed.
* @return the value of the function.
* @throws IllegalArgumentException when the activated method itself can
* ascertain that a precondition, specified in the API expressed at the
* level of the activated method, has been violated.
* When Commons Math throws an {@code IllegalArgumentException}, it is
* usually the consequence of checking the actual parameters passed to
* the method.
*/
double value(double x);
}
Math, 29
<FILEB>
<CHANGES>
final int n = getDimension();
for (int i = 0; i < n; i++) {
res.setEntry(i, this.getEntry(i) / v.getEntry(i));
<CHANGEE>
<CHANGES>
if (v.isNaN() || v.isInfinite()) {
final int n = getDimension();
for (int i = 0; i < n; i++) {
final double y = v.getEntry(i);
if (Double.isNaN(y)) {
res.setEntry(i, Double.NaN);
} else if (Double.isInfinite(y)) {
final double x = this.getEntry(i);
res.setEntry(i, x * y);
}
}
}
<CHANGEE>
<FILEE>
<FILEB>
@Override
public double dotProduct(RealVector v) {
if(v instanceof OpenMapRealVector) {
return dotProduct((OpenMapRealVector)v);
} else {
return super.dotProduct(v);
}
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeDivide(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
/*
* MATH-803: it is not sufficient to loop through non zero entries of
* this only. Indeed, if this[i] = 0d and v[i] = 0d, then
* this[i] / v[i] = NaN, and not 0d.
*/
<CHANGES>
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() / v.getEntry(iter.key()));
<CHANGEE>
}
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeMultiply(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() * v.getEntry(iter.key()));
}
/*
* MATH-803: the above loop assumes that 0d * x = 0d for any double x,
* which allows to consider only the non-zero entries of this. However,
* this fails if this[i] == 0d and (v[i] = NaN or v[i] = Infinity).
*
* These special cases are handled below.
*/
<CHANGES>
<CHANGEE>
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector getSubVector(int index, int n) {
checkIndex(index);
if (n < 0) {
throw new NotPositiveException(LocalizedFormats.NUMBER_OF_ELEMENTS_SHOULD_BE_POSITIVE, n);
}
checkIndex(index + n - 1);
OpenMapRealVector<SCANS> of two for bit mask to work properly. </p>
*/
private static final int DEFAULT_EXPECTED_SIZE = 16;
/** Multiplier for size growth when map fills up.
* <p>This must be a power of two for bit mask to work properly. </p>
*/
private static final int RESIZE_MULTIPLIER = 2;
/** Number of bits to perturb the index when probing for collision resolution. */
private static final int PERTURB_SHIFT = 5;
/** Keys table. */
private int[] keys;
/** Values table. */
private double[] values;
/** States table. */
private byte[] states;
/** Return value for missing entries. */
private final double missingEntries;
/** Current size of the map. */
private int size;
/** Bit mask for hash values. */
private int mask;
/** Modifications count. */
private transient int count;
/**
* Build an empty map with default size and using NaN for missing entries.
*/
public OpenIntToDoubleHashMap() {
this(DEFAULT_EXPECTED_SIZE, Double.NaN);
}
/**
* Build an empty map with default size
* @param missingEntries value to return when a missing entry is fetched
*/
public OpenIntToDoubleHashMap(final double missingEntries) {
this(DEFAULT_EXPECTED_SIZE, missingEntries);
}
/**
* Build an empty map with specified size and using NaN for missing entries.
* @param expectedSize expected number of elements in the map
*/
public OpenIntToDoubleHashMap(final int expectedSize) {
this(expectedSize, Double.NaN);
}
/**
* Build an empty map with specified size.
* @param expectedSize expected number of elements in the map
* @param missingEntries value to return when a missing entry is fetched
*/
public OpenIntToDoubleHashMap(final int expectedSize,
final double missingEntries) {
final int capacity = computeCapacity(expectedSize);
keys = new int[capacity];
values = new double[capacity];
states = new byte[capacity];
this.missingEntries = missingEntries;
mask = capacity - 1;
}
/**
* Copy constructor.
* @param source map to copy
*/
public OpenIntToDoubleHashMap(final OpenIntToDoubleHashMap source
Math, 29
<FILEB>
<CHANGES>
final int n = getDimension();
for (int i = 0; i < n; i++) {
res.setEntry(i, this.getEntry(i) / v.getEntry(i));
<CHANGEE>
<CHANGES>
if (v.isNaN() || v.isInfinite()) {
final int n = getDimension();
for (int i = 0; i < n; i++) {
final double y = v.getEntry(i);
if (Double.isNaN(y)) {
res.setEntry(i, Double.NaN);
} else if (Double.isInfinite(y)) {
final double x = this.getEntry(i);
res.setEntry(i, x * y);
}
}
}
<CHANGEE>
<FILEE>
<FILEB>
@Override
public double dotProduct(RealVector v) {
if(v instanceof OpenMapRealVector) {
return dotProduct((OpenMapRealVector)v);
} else {
return super.dotProduct(v);
}
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeDivide(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
/*
* MATH-803: it is not sufficient to loop through non zero entries of
* this only. Indeed, if this[i] = 0d and v[i] = 0d, then
* this[i] / v[i] = NaN, and not 0d.
*/
<CHANGES>
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() / v.getEntry(iter.key()));
<CHANGEE>
}
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeMultiply(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() * v.getEntry(iter.key()));
}
/*
* MATH-803: the above loop assumes that 0d * x = 0d for any double x,
* which allows to consider only the non-zero entries of this. However,
* this fails if this[i] == 0d and (v[i] = NaN or v[i] = Infinity).
*
* These special cases are handled below.
*/
<CHANGES>
<CHANGEE>
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector getSubVector(int index, int n) {
checkIndex(index);
if (n < 0) {
throw new NotPositiveException(LocalizedFormats.NUMBER_OF_ELEMENTS_SHOULD_BE_POSITIVE, n);
}
checkIndex(index + n - 1);
OpenMapRealVector<SCANS>) {
final int length = source.keys.length;
keys = new int[length];
System.arraycopy(source.keys, 0, keys, 0, length);
values = new double[length];
System.arraycopy(source.values, 0, values, 0, length);
states = new byte[length];
System.arraycopy(source.states, 0, states, 0, length);
missingEntries = source.missingEntries;
size = source.size;
mask = source.mask;
count = source.count;
}
/**
* Compute the capacity needed for a given size.
* @param expectedSize expected size of the map
* @return capacity to use for the specified size
*/
private static int computeCapacity(final int expectedSize) {
if (expectedSize == 0) {
return 1;
}
final int capacity = (int) FastMath.ceil(expectedSize / LOAD_FACTOR);
final int powerOfTwo = Integer.highestOneBit(capacity);
if (powerOfTwo == capacity) {
return capacity;
}
return nextPowerOfTwo(capacity);
}
/**
* Find the smallest power of two greater than the input value
* @param i input value
* @return smallest power of two greater than the input value
*/
private static int nextPowerOfTwo(final int i) {
return Integer.highestOneBit(i) << 1;
}
/**
* Get the stored value associated with the given key
* @param key key associated with the data
* @return data associated with the key
*/
public double get(final int key) {
final int hash = hashOf(key);
int index = hash & mask;
if (containsKey(key, index)) {
return values[index];
}
if (states[index] == FREE) {
return missingEntries;
}
int j = index;
for (int perturb = perturb(hash); states[index] != FREE; perturb >>= PERTURB_SHIFT) {
j = probe(perturb, j);
index = j & mask;
if (containsKey(key, index)) {
return values[index];
}
}
return missingEntries;
}
/**
* Check
Math, 29
<FILEB>
<CHANGES>
final int n = getDimension();
for (int i = 0; i < n; i++) {
res.setEntry(i, this.getEntry(i) / v.getEntry(i));
<CHANGEE>
<CHANGES>
if (v.isNaN() || v.isInfinite()) {
final int n = getDimension();
for (int i = 0; i < n; i++) {
final double y = v.getEntry(i);
if (Double.isNaN(y)) {
res.setEntry(i, Double.NaN);
} else if (Double.isInfinite(y)) {
final double x = this.getEntry(i);
res.setEntry(i, x * y);
}
}
}
<CHANGEE>
<FILEE>
<FILEB>
@Override
public double dotProduct(RealVector v) {
if(v instanceof OpenMapRealVector) {
return dotProduct((OpenMapRealVector)v);
} else {
return super.dotProduct(v);
}
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeDivide(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
/*
* MATH-803: it is not sufficient to loop through non zero entries of
* this only. Indeed, if this[i] = 0d and v[i] = 0d, then
* this[i] / v[i] = NaN, and not 0d.
*/
<CHANGES>
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() / v.getEntry(iter.key()));
<CHANGEE>
}
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector ebeMultiply(RealVector v) {
checkVectorDimensions(v.getDimension());
OpenMapRealVector res = new OpenMapRealVector(this);
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
res.setEntry(iter.key(), iter.value() * v.getEntry(iter.key()));
}
/*
* MATH-803: the above loop assumes that 0d * x = 0d for any double x,
* which allows to consider only the non-zero entries of this. However,
* this fails if this[i] == 0d and (v[i] = NaN or v[i] = Infinity).
*
* These special cases are handled below.
*/
<CHANGES>
<CHANGEE>
return res;
}
/** {@inheritDoc} */
@Override
public OpenMapRealVector getSubVector(int index, int n) {
checkIndex(index);
if (n < 0) {
throw new NotPositiveException(LocalizedFormats.NUMBER_OF_ELEMENTS_SHOULD_BE_POSITIVE, n);
}
checkIndex(index + n - 1);
OpenMapRealVector<SCANS>_MULTIPLIER * oldLength;
final int[] newKeys = new int[newLength];
final double[] newValues = new double[newLength];
final byte[] newStates = new byte[newLength];
final int newMask = newLength - 1;
for (int i = 0; i < oldLength; ++i) {
if (oldStates[i] == FULL) {
final int key = oldKeys[i];
final int index = findInsertionIndex(newKeys, newStates, key, newMask);
newKeys[index] = key;
newValues[index] = oldValues[i];
newStates[index] = FULL;
}
}
mask = newMask;
keys = newKeys;
values = newValues;
states = newStates;
}
/**
* Check if tables should grow due to increased size.
* @return true if tables should grow
*/
private boolean shouldGrowTable() {
return size > (mask + 1) * LOAD_FACTOR;
}
/**
* Compute the hash value of a key
* @param key key to hash
* @return hash value of the key
*/
private static int hashOf(final int key) {
final int h = key ^ ((key >>> 20) ^ (key >>> 12));
return h ^ (h >>> 7) ^ (h >>> 4);
}
/** Iterator class for the map. */
public class Iterator {
/** Reference modification count. */
private final int referenceCount;
/** Index of current element. */
private int current;
/** Index of next element. */
private int next;
/**
* Simple constructor.
*/
private Iterator() {
// preserve the modification count of the map to detect concurrent modifications later
referenceCount = count;
// initialize current index
next = -1;
try {
advance();
} catch (NoSuchElementException nsee) {
// ignored
}
}
/**
* Check if there is a next element in the map.
* @return true if there is a next element
*/
public boolean hasNext() {
return next >= 0;
}
/**
* Get the key of current entry.
* @return key of current entry
* @exception ConcurrentModificationException if the